教练,我想学二叉树遍历!

二叉树具有前序、中序、后序三种遍历方式。递归的相信大家都会写,但是迭代的该怎么写呢?怎样的迭代写法才能具有统一性便于理解呢?请看下面具体的做法,每种遍历方式各有2种策略,基于栈的和基于游标指针,使用栈来辅助的。

1.前序遍历

不采用游标指针的做法

void preOrder(TreeNode* root,vector<int>& res){        
    if(root == nullptr) return;                        
    stack<TreeNode*> st;                               
    st.push(root);                                     
    while(!st.empty()){                                
        TreeNode* cur = st.top();st.pop();             
        res.push_back(cur->val);                       
        if(cur->right) st.push(cur->right);            
        if(cur->left) st.push(cur->left);              
    }                                                  
    return;                                            
}                                                                        

采用游标指针的做法

void preOrder(TreeNode* root,vector<int>& res){         
    if(root == nullptr) return;                        
    stack<TreeNode*> st;                               
    TreeNode* cur = root;                              
    while(!st.empty() || cur != nullptr){              
        if(cur){                  
            res.push_back(cur->val);                      
            st.push(cur);                              
            cur = cur->left;                           
        }else{                                         
            cur = st.top();st.pop();                                
            cur = cur->right;                          
        }                                              
    }                                                  
    return;                                            
}                                                      

2.中序遍历

不采用游标指针的做法,这里只能贴个错误的,因为目前看没有这样的解法

void inOrder2(TreeNode* root,vector<int>& res){                                               
    if(root == nullptr) return;                                                               
    stack<TreeNode*> st;                                                                      
    st.push(root);                                                                            
    TreeNode* pre = root;                                                                     
    while (!st.empty()) {                                                                     
        TreeNode* p = st.top();                                                               
        if(p->left && pre != p->left){//这里很难判断左子树是否被访问过,除非你每个节点搞个变量                           
            st.push(p->left);                                                                 
        }else{                                                                                
            st.pop();                                                                         
            res.push_back(p->val);                                                            
            pre = p;                                                                          
            if(p->right) {                                                                    
                st.push(p->right);                                                            
            }                                                                                 
        }                                                                                     
    }                                                                                         
    return ;                                                                                  
}                                                                                             

采用游标指针的做法

 void inOrder(TreeNode* root,vector<int>& res){    
     if(root == nullptr) return;                   
     stack<TreeNode*> st;                          
     TreeNode* cur = root;                         
     while(!st.empty() || cur != nullptr){         
         if(cur){                                  
             st.push(cur);                         
             cur = cur->left;                      
         }else{                                    
             cur = st.top();st.pop();              
             res.push_back(cur->val);              
             cur = cur->right;                     
         }                                         
     }                                             
     return;                                       
 }                                                 

3.后序遍历

不采用游标指针的做法

 void postOrder(TreeNode* root,vector<int>& res){             
     if(root == nullptr) return;                               
     stack<TreeNode*> st;                                      
     TreeNode* pre = root;//                                   
     st.push(root);                                            
     while(!st.empty()){                                       
         TreeNode* cur = st.top();
        // pre = cur->left说明cur没有右子树 ,也可以处理cur了                                  
         if((cur->left == nullptr && cur->right == nullptr)    
                 ||pre == cur->left||pre == cur->right){ 
             res.push_back(cur->val);                          
             st.pop();                                         
             pre = cur;                                        
         }else{                                                
             if(cur->right)  st.push(cur->right);              
             if(cur->left)   st.push(cur->left);               
         }                                                     
     }                                                         
     return;                                                   
 }                                                             

采用游标指针的做法

 vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == nullptr) return res;
        TreeNode* cur = root;
        TreeNode* pre = root;
        stack<TreeNode*> st;
        while(st.empty() == false || cur != nullptr){
            if(cur != nullptr){
                st.push(cur);
                cur = cur->left;
            }else{
                cur = st.top();
                if(cur->right == nullptr || cur->right == pre){
                    res.push_back(cur->val);
                    st.pop();
                    pre = cur;
                    cur = nullptr;
                }else{
                    cur = cur->right;
                }
            }
        }
        return res;
    }                                                         

总结一下这2种策略的差别以及关键——基于栈的策略,就想办法在每一次迭代处理好栈;基于游标指针的策略,就在每一步处理好游标指针,只在需要的时候去处理辅助栈。听起来像是废话,但是带着这2句话反过头来看代码,会有感悟的。

衣带渐宽终不悔,为伊消得人憔悴。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值