037.二叉树非递归遍历
用栈实现非递归遍历
-
时间复杂度
O(n) -
空间复杂度
O(h),h为树高
前序
struct TreeNode{
int val;
TreeNode*left;
TreeNode*right;
TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};
void preorder(TreeNode*root){
if(root==nullptr)return;
stack<TreeNode*>st;
st.push(root);
while(!st.empty()){
TreeNode* cur=st.top();
st.pop();
/*
前序位置
*/
if(cur->right!=nullptr){
st.push(cur->right);
}
if(cur->left!=nullptr){
st.push(cur->left);
}
}
}
中序
void inorder(TreeNode*root){
if(root==nullptr)return;
stack<TreeNode*>st;
TreeNode*cur=root;
while(!st.empty()||cur!=nullptr){
if(cur!=nullptr){
st.push(cur);
cur=cur->left;
}
else{
cur=st.top();
st.pop();
/*
中序位置
*/
cur=cur->right;
}
}
}
后序
void postorder(TreeNode*root){
if(root==nullptr)return;
stack<TreeNode*>st;
st.push(root);
while(!st.empty()){
TreeNode*cur=st.top();
if(cur->left!=nullptr&&cur->left!=root&&cur->right!=root){
st.push(cur->left);
}
else if(cur->right!=nullptr&&cur->right!=root){
st.push(cur->right);
}
else{
/*
后序位置
*/
root=st.top();
st.pop();
}
}
}
I am the bone of my sword

浙公网安备 33010602011771号