NC45_实现二叉树先序、中序和后序遍历
知识点:二叉树、递归、stack
题目链接
题目描述
分别按照二叉树先序,中序和后序打印所有的节点。
输入:
{1,2,3}
输出:
[[1,2,3],[2,1,3],[2,3,1]]
备注:
n≤106
解题思路
先序迭代版本
- 调用stack
- 一边放入答案数组 一边放到stack中 一直向左
- tmp = tmp->right 到右子树
中序迭代版本:
- 调用stack
- 左边有节点的话 一直放到stack中 一直向左
- 取出stack的头节点tmp,放入答案数组
- tmp = tmp->right 到右子树
后序迭代版本:
- 采用一个节点记录上一个遍历的节点,对于根节点难以判断是从左子树返回来的 还是右子树来的
- 当该节点的右节点是上一个遍历的 说明这个节点左右子树已经遍历过了
代码
#include "cheader.h"
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};
class Solution {
private:
vector<vector<int>> ans;
vector<int> pre;
vector<int> in;
vector<int> post;
public:
/**
*
* @param root TreeNode类 the root of binary tree
* @return int整型vector<vector<>>
*/
vector<vector<int> > threeOrders(TreeNode* root) {
preorder2(root);
inorder2(root);
postorder2(root);
ans = {pre,in,post};
return ans;
}
void preorder1(TreeNode *root){
if(root == nullptr)
return;
pre.push_back(root->val);
preorder1(root->left);
preorder1(root->right);
}
void preorder2(TreeNode *root){
stack<TreeNode*> sta;
TreeNode *tmp = root;
while(!sta.empty() || tmp!=nullptr){
while(tmp != nullptr){
sta.push(tmp);
pre.push_back(tmp->val);
tmp = tmp->left;
}
tmp = sta.top();sta.pop();
tmp = tmp->right;
}
}
void inorder1(TreeNode *root){
if(root == nullptr)
return;
inorder1(root->left);
in.push_back(root->val);
inorder1(root->right);
}
void inorder2(TreeNode *root){
stack<TreeNode*> sta;
TreeNode *tmp = root;
while(!sta.empty() || tmp!=nullptr){
while(tmp != nullptr){
sta.push(tmp);
tmp = tmp->left;
}
tmp = sta.top();sta.pop();
in.push_back(tmp->val);
tmp = tmp->right;
}
}
void postorder1(TreeNode *root){
if(root == nullptr)
return;
postorder1(root->left);
postorder1(root->right);
post.push_back(root->val);
}
void postorder2(TreeNode *root){
stack<TreeNode*> sta;
TreeNode *tmp = root;
TreeNode *pre = nullptr;
while(!sta.empty() || tmp!=nullptr){
while(tmp != nullptr){
sta.push(tmp);
tmp = tmp->left;
}
tmp = sta.top();sta.pop();
if(tmp->right == nullptr || tmp->right == pre){
post.push_back(tmp->val);
pre = tmp;
tmp = nullptr; //防止重复添加
}
else{
sta.push(tmp);//该节点还有右子树没有遍历完,继续扔在stack中
tmp = tmp->right;//到右子树中去
}
}
}
};
int main()
{
TreeNode root(1);TreeNode left(2);TreeNode right(3);
root.left = &left; root.right = &right;
Solution s;
vector<vector<int>> ans = s.threeOrders(&root);
for(vector<int> x :ans){
for(int d : x)
cout<<d<<" ";
cout<<endl;
}
return 0;
}
今天也是爱zz的一天