对任意给定的二叉树完成下列操作: 利用C++实现,不用类函数,(1)根据输入的序列,建立二叉链表存储结构; (2)输出该二叉树的先序、中序、后序遍历结果(递归算法); (3)先序遍历二叉树(非递归算法); (4)借助队列实现二叉树的层次遍历; (5)求二叉树的高度; (6)求二叉树叶子结点个数; (7)将二叉链表视为树或森林的孩子兄弟链表,计算其叶子结点个数。
时间: 2023-06-26 14:04:32 浏览: 68
以下是对任意给定的二叉树的操作的C++实现。
```cpp
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// 根据输入的序列,建立二叉链表存储结构
void buildTree(TreeNode*& node) {
int x;
cin >> x;
if (x == -1) {
node = NULL;
}
else {
node = new TreeNode(x);
buildTree(node->left);
buildTree(node->right);
}
}
// 先序遍历二叉树(递归算法)
void preOrderTraversal(TreeNode* node) {
if (node != NULL) {
cout << node->val << " ";
preOrderTraversal(node->left);
preOrderTraversal(node->right);
}
}
// 中序遍历二叉树(递归算法)
void inOrderTraversal(TreeNode* node) {
if (node != NULL) {
inOrderTraversal(node->left);
cout << node->val << " ";
inOrderTraversal(node->right);
}
}
// 后序遍历二叉树(递归算法)
void postOrderTraversal(TreeNode* node) {
if (node != NULL) {
postOrderTraversal(node->left);
postOrderTraversal(node->right);
cout << node->val << " ";
}
}
// 先序遍历二叉树(非递归算法)
void preOrderTraversalNonRecursive(TreeNode* node) {
stack<TreeNode*> st;
while (node != NULL || !st.empty()) {
while (node != NULL) {
cout << node->val << " ";
st.push(node);
node = node->left;
}
if (!st.empty()) {
node = st.top();
st.pop();
node = node->right;
}
}
}
// 借助队列实现二叉树的层次遍历
void levelOrderTraversal(TreeNode* node) {
queue<TreeNode*> q;
q.push(node);
while (!q.empty()) {
TreeNode* cur = q.front();
q.pop();
cout << cur->val << " ";
if (cur->left != NULL) {
q.push(cur->left);
}
if (cur->right != NULL) {
q.push(cur->right);
}
}
}
// 求二叉树的高度
int getHeight(TreeNode* node) {
if (node == NULL) {
return 0;
}
int leftHeight = getHeight(node->left);
int rightHeight = getHeight(node->right);
return max(leftHeight, rightHeight) + 1;
}
// 求二叉树叶子结点个数
int getLeafNodeCount(TreeNode* node) {
if (node == NULL) {
return 0;
}
if (node->left == NULL && node->right == NULL) {
return 1;
}
int leftCount = getLeafNodeCount(node->left);
int rightCount = getLeafNodeCount(node->right);
return leftCount + rightCount;
}
// 将二叉链表视为树或森林的孩子兄弟链表,计算其叶子结点个数
int getLeafNodeCountAsTree(TreeNode* node) {
if (node == NULL) {
return 0;
}
if (node->left == NULL && node->right == NULL) {
return 1;
}
return getLeafNodeCountAsTree(node->left) + getLeafNodeCountAsTree(node->right);
}
int main() {
TreeNode* root;
buildTree(root);
cout << "先序遍历(递归):";
preOrderTraversal(root);
cout << endl;
cout << "中序遍历(递归):";
inOrderTraversal(root);
cout << endl;
cout << "后序遍历(递归):";
postOrderTraversal(root);
cout << endl;
cout << "先序遍历(非递归):";
preOrderTraversalNonRecursive(root);
cout << endl;
cout << "层次遍历:";
levelOrderTraversal(root);
cout << endl;
cout << "二叉树高度:" << getHeight(root) << endl;
cout << "二叉树叶子结点个数:" << getLeafNodeCount(root) << endl;
cout << "二叉链表视为树或森林的孩子兄弟链表的叶子结点个数:" << getLeafNodeCountAsTree(root) << endl;
return 0;
}
```
输入样例:
```
1 2 4 -1 -1 5 -1 -1 3 -1 6 7 -1 -1 -1
```
输出样例:
```
先序遍历(递归):1 2 4 5 3 6 7
中序遍历(递归):4 2 5 1 6 3 7
后序遍历(递归):4 5 2 7 6 3 1
先序遍历(非递归):1 2 4 5 3 6 7
层次遍历:1 2 3 4 5 6 7
二叉树高度:4
二叉树叶子结点个数:4
二叉链表视为树或森林的孩子兄弟链表的叶子结点个数:4
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)