用C++写输入完全二叉树的先序序列,用 # 代表虚结点(空指针), 如 ABD###CE##F###建立二叉树,实现先序、中序和后序以及按层次遍历 序列 求所有叶子及结点总数
时间: 2024-02-12 10:07:19 浏览: 69
好的,以下是使用C++实现题目要求的代码:
```cpp
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
char val;
TreeNode* left;
TreeNode* right;
TreeNode(char x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* buildTree(vector<char>& preorder, int& i) {
if (i >= preorder.size() || preorder[i] == '#') {
i++;
return NULL;
}
TreeNode* root = new TreeNode(preorder[i++]);
root->left = buildTree(preorder, i);
root->right = buildTree(preorder, i);
return root;
}
vector<char> preorderTraversal(TreeNode* root) {
vector<char> res;
if (!root) return res;
res.push_back(root->val);
vector<char> left = preorderTraversal(root->left);
res.insert(res.end(), left.begin(), left.end());
vector<char> right = preorderTraversal(root->right);
res.insert(res.end(), right.begin(), right.end());
return res;
}
vector<char> inorderTraversal(TreeNode* root) {
vector<char> res;
if (!root) return res;
vector<char> left = inorderTraversal(root->left);
res.insert(res.end(), left.begin(), left.end());
res.push_back(root->val);
vector<char> right = inorderTraversal(root->right);
res.insert(res.end(), right.begin(), right.end());
return res;
}
vector<char> postorderTraversal(TreeNode* root) {
vector<char> res;
if (!root) return res;
vector<char> left = postorderTraversal(root->left);
res.insert(res.end(), left.begin(), left.end());
vector<char> right = postorderTraversal(root->right);
res.insert(res.end(), right.begin(), right.end());
res.push_back(root->val);
return res;
}
vector<vector<char>> levelOrder(TreeNode* root) {
vector<vector<char>> res;
if (!root) return res;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
vector<char> level;
for (int i = q.size(); i > 0; i--) {
TreeNode* node = q.front(); q.pop();
level.push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
res.push_back(level);
}
return res;
}
pair<int, int> countNodesAndLeaves(TreeNode* root) {
if (!root) return {0, 0};
if (!root->left && !root->right) return {1, 1};
auto left = countNodesAndLeaves(root->left);
auto right = countNodesAndLeaves(root->right);
int nodes = left.first + right.first + 1;
int leaves = left.second + right.second;
return {nodes, leaves};
}
int main() {
vector<char> preorder{'A', 'B', 'D', '#', '#', '#', 'C', 'E', '#', '#', 'F', '#', '#', '#'};
int i = 0;
TreeNode* root = buildTree(preorder, i);
vector<char> preorderRes = preorderTraversal(root);
vector<char> inorderRes = inorderTraversal(root);
vector<char> postorderRes = postorderTraversal(root);
vector<vector<char>> levelOrderRes = levelOrder(root);
pair<int, int> countRes = countNodesAndLeaves(root);
int nodes = countRes.first;
int leaves = countRes.second;
cout << "先序遍历序列:";
for (auto c : preorderRes) cout << c << " ";
cout << endl;
cout << "中序遍历序列:";
for (auto c : inorderRes) cout << c << " ";
cout << endl;
cout << "后序遍历序列:";
for (auto c : postorderRes) cout << c << " ";
cout << endl;
cout << "按层次遍历序列:" << endl;
for (auto level : levelOrderRes) {
for (auto c : level) {
cout << c << " ";
}
cout << endl;
}
cout << "节点总数:" << nodes << endl;
cout << "叶子节点数:" << leaves << endl;
return 0;
}
```
希望这份代码可以帮助到您!
阅读全文