#include<iostream>
#include<vector>
#include<stack>
using namespace std;
class TreeNode {
public:
int val;
TreeNode* right;
TreeNode* left;
TreeNode(int value) : val(value), right(nullptr), left(nullptr) {
}
};
TreeNode* BulidTreeNode(vector<int> vec, int index) {
if (index >= vec.size() || vec[index] == -1) {
return nullptr;
}
TreeNode* node = new TreeNode(vec[index]);
node->left = BulidTreeNode(vec, index * 2 + 1);
node->right = BulidTreeNode(vec, index * 2 + 2);
return node;
}
void printmid(TreeNode* root) {
if (root == nullptr) {
return;
}
else {
printmid(root->left);
cout << root->val << endl;
printmid(root->right);
}
}
void printfront(TreeNode* root) {
if (root == nullptr) {
return;
}
else {
cout << root->val << endl;
printfront(root->left);
printfront(root->right);
}
}
void printend(TreeNode* root) {
if (root == nullptr) {
return;
}
else {
printend(root->left);
printend(root->right);
cout << root->val << endl;
}
}
int main() {
int n;
cin >> n;
vector<int> vec;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
vec.push_back(num);
}
TreeNode* root = BulidTreeNode(vec, 0);
printmid(root);
cout << "====================" << endl;
printfront(root);
cout << "====================" << endl;
printend(root);
cout << "====================" << endl;
return 0;
}
2438

被折叠的 条评论
为什么被折叠?



