二叉树part01
二叉树的前中后容易混淆,这里前中后,其实指的就是中间节点的遍历顺序。
如果是用递归的方法,那么代码实现方式如下:
#include<iostream>
using namespace std;
#include<vector>
#include<queue>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
//前序遍历
void traversal_qian(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
vec.push_back(cur->val); // 中
traversal_qian(cur->left, vec); // 左
traversal_qian(cur->right, vec); // 右
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
traversal_qian(root, result);
return result;
}
//中序遍历
void traversal_zhong(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal_zhong(cur->left, vec); // 左
vec.push_back(cur->val); // 中
traversal_zhong(cur->right, vec); // 右
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
traversal_zhong(root, result);
return result;
}
//后序遍历
void traversal_hou(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal_hou(cur->left, vec); // 左
traversal_hou(cur->right, vec); // 右
vec.push_back(cur->val); // 中
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
traversal_hou(root, result);
return result;
}
void treePrint(TreeNode* root) {
if (!root) return;
queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (!node) cout << "-1 ";
else {
cout << node->val << " ";
que.push(node->left);
que.push(node->right);
}
}
cout << endl;
}
}
//需要补齐-1当前节点的左右节点
TreeNode* build(const vector<int>& nums) {
vector<TreeNode*> tree;
for (auto x : nums) {
if (x == -1) tree.push_back(nullptr);
else tree.push_back(new TreeNode(x));
}
int idx = 1;
for (int i = 0; i < tree.size(); i++) {
if (tree[i] == nullptr) continue;
if (idx >= tree.size()) break;
tree[i]->left = tree[idx];
tree[i]->right = tree[idx + 1];
idx += 2;
}
return tree[0];
}
int main() {
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
cout << endl;
TreeNode* root = build(nums);
treePrint(root);
cout << "前序遍历:" << endl;
vector<int> qian = preorderTraversal(root);
for (auto iter : qian)
{
cout << iter << ",";
}
cout<<endl << "中序遍历:" << endl;
vector<int> zhong = inorderTraversal(root);
for (auto iter : zhong)
{
cout << iter << ",";
}
cout<<endl << "后序遍历:" << endl;
vector<int> hou = postorderTraversal(root);
for (auto iter : hou)
{
cout << iter << ",";
}
system("pause");
return 0;
}
如果用迭代法,那么中序遍历是到达树左面的最底部,再开始处理节点。对于后序遍历,先序遍历是中左右,后续遍历是左右中。从中左右–(调整代码左右循环)–>中右左–(反转result数组)–>左右中,调整一下先序遍历的代码顺序即可。
#include<iostream>
using namespace std;
#include<vector>
#include<queue>
#include<stack>
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
//前序遍历
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); // 中
st.pop();
result.push_back(node->val);
if (node->right) st.push(node->right); // 右(空节点不入栈)
if (node->left) st.push(node->left); // 左(空节点不入栈)
}
return result;
}
//中序遍历
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur != NULL || !st.empty()) {
if (cur != NULL) { // 指针来访问节点,访问到最底层
st.push(cur); // 将访问的节点放进栈
cur = cur->left; // 左
}
else {
cur = st.top(); // 从栈里弹出的数据,就是要处理的数据(放进result数组里的数据)
st.pop();
result.push_back(cur->val); // 中
cur = cur->right; // 右
}
}
return result;
}
//后序遍历
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
st.pop();
result.push_back(node->val);
if (node->left) st.push(node->left); // 相对于前序遍历,这更改一下入栈顺序 (空节点不入栈)
if (node->right) st.push(node->right); // 空节点不入栈
}
reverse(result.begin(), result.end()); // 将结果反转之后就是左右中的顺序了
return result;
}
void treePrint(TreeNode* root) {
if (!root) return;
queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (!node) cout << "-1 ";
else {
cout << node->val << " ";
que.push(node->left);
que.push(node->right);
}
}
cout << endl;
}
}
//需要补齐-1当前节点的左右节点
TreeNode* build(const vector<int>& nums) {
vector<TreeNode*> tree;
for (auto x : nums) {
if (x == -1) tree.push_back(nullptr);
else tree.push_back(new TreeNode(x));
}
int idx = 1;
for (int i = 0; i < tree.size(); i++) {
if (tree[i] == nullptr) continue;
if (idx >= tree.size()) break;
tree[i]->left = tree[idx];
tree[i]->right = tree[idx + 1];
idx += 2;
}
return tree[0];
}
int main() {
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
cout << endl;
TreeNode* root = build(nums);
treePrint(root);
cout << "前序遍历:" << endl;
vector<int> qian = preorderTraversal(root);
for (auto iter : qian)
{
cout << iter << ",";
}
cout<<endl << "中序遍历:" << endl;
vector<int> zhong = inorderTraversal(root);
for (auto iter : zhong)
{
cout << iter << ",";
}
cout<<endl << "后序遍历:" << endl;
vector<int> hou = postorderTraversal(root);
for (auto iter : hou)
{
cout << iter << ",";
}
system("pause");
return 0;
}