以二叉链表为存储结构,完成以下功能: 以先序递归方式创建一棵二叉树;输出二叉树的先序、中序和后序递归或非递归遍历下的结点访问次序;输出二叉树所有的叶子节点和叶子节点个数;输出二叉树的高度;输出二叉树的按层次遍历序列(提示:实现需用到队列);
时间: 2024-01-22 22:19:55 浏览: 90
以下是基于C++语言的实现:
```cpp
#include <iostream>
#include <queue>
using namespace std;
// 二叉树结点定义
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 以先序递归方式创建一棵二叉树
TreeNode* createTree() {
int val;
cin >> val;
if (val == -1) {
return nullptr; // -1表示空结点
}
TreeNode* root = new TreeNode(val);
root->left = createTree();
root->right = createTree();
return root;
}
// 先序递归遍历二叉树
void preOrderTraversalRecursive(TreeNode* root) {
if (!root) {
return;
}
cout << root->val << " ";
preOrderTraversalRecursive(root->left);
preOrderTraversalRecursive(root->right);
}
// 中序递归遍历二叉树
void inOrderTraversalRecursive(TreeNode* root) {
if (!root) {
return;
}
inOrderTraversalRecursive(root->left);
cout << root->val << " ";
inOrderTraversalRecursive(root->right);
}
// 后序递归遍历二叉树
void postOrderTraversalRecursive(TreeNode* root) {
if (!root) {
return;
}
postOrderTraversalRecursive(root->left);
postOrderTraversalRecursive(root->right);
cout << root->val << " ";
}
// 先序非递归遍历二叉树
void preOrderTraversalNonRecursive(TreeNode* root) {
if (!root) {
return;
}
stack<TreeNode*> s;
s.push(root);
while (!s.empty()) {
TreeNode* curr = s.top();
s.pop();
cout << curr->val << " ";
if (curr->right) {
s.push(curr->right); // 先访问右子树
}
if (curr->left) {
s.push(curr->left);
}
}
}
// 中序非递归遍历二叉树
void inOrderTraversalNonRecursive(TreeNode* root) {
if (!root) {
return;
}
stack<TreeNode*> s;
TreeNode* curr = root;
while (curr || !s.empty()) {
while (curr) {
s.push(curr);
curr = curr->left; // 一直往左走
}
curr = s.top();
s.pop();
cout << curr->val << " ";
curr = curr->right; // 转向右子树
}
}
// 后序非递归遍历二叉树
void postOrderTraversalNonRecursive(TreeNode* root) {
if (!root) {
return;
}
stack<TreeNode*> s;
TreeNode* prev = nullptr;
while (root || !s.empty()) {
while (root) {
s.push(root);
root = root->left;
}
TreeNode* top = s.top();
if (top->right && top->right != prev) {
root = top->right; // 左子树遍历完毕,转向右子树
} else {
cout << top->val << " ";
prev = top;
s.pop();
}
}
}
// 输出二叉树所有的叶子节点和叶子节点个数
void printLeaves(TreeNode* root, int& count) {
if (!root) {
return;
}
if (!root->left && !root->right) {
cout << root->val << " ";
count++;
}
printLeaves(root->left, count);
printLeaves(root->right, count);
}
// 输出二叉树的高度
int getHeight(TreeNode* root) {
if (!root) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return max(leftHeight, rightHeight) + 1;
}
// 输出二叉树的按层次遍历序列
void levelOrderTraversal(TreeNode* root) {
if (!root) {
return;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* curr = q.front();
q.pop();
cout << curr->val << " ";
if (curr->left) {
q.push(curr->left);
}
if (curr->right) {
q.push(curr->right);
}
}
}
int main() {
TreeNode* root = createTree();
cout << "先序递归遍历:";
preOrderTraversalRecursive(root);
cout << endl;
cout << "中序递归遍历:";
inOrderTraversalRecursive(root);
cout << endl;
cout << "后序递归遍历:";
postOrderTraversalRecursive(root);
cout << endl;
cout << "先序非递归遍历:";
preOrderTraversalNonRecursive(root);
cout << endl;
cout << "中序非递归遍历:";
inOrderTraversalNonRecursive(root);
cout << endl;
cout << "后序非递归遍历:";
postOrderTraversalNonRecursive(root);
cout << endl;
int count = 0;
cout << "叶子节点为:";
printLeaves(root, count);
cout << endl << "叶子节点个数为:" << count << endl;
cout << "二叉树的高度为:" << getHeight(root) << endl;
cout << "按层次遍历序列为:";
levelOrderTraversal(root);
cout << endl;
return 0;
}
```
输入格式:
```
1 2 -1 -1 3 4 -1 -1 5 -1 -1
```
输出格式:
```
先序递归遍历:1 2 3 4 5
中序递归遍历:2 1 4 3 5
后序递归遍历:2 4 5 3 1
先序非递归遍历:1 2 3 4 5
中序非递归遍历:2 1 4 3 5
后序非递归遍历:2 4 5 3 1
叶子节点为:2 4 5
叶子节点个数为:3
二叉树的高度为:3
按层次遍历序列为:1 2 3 4 5
```
阅读全文