LeetCode -- 二叉树的后序遍历(最详细的解法!!!)

给定一个二叉树,返回它的 后序 遍历。

示例:

输入: [1,null,2,3]
1

2
/
3

输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?

解题思路:总体思路分为递归和迭代两种。两者都是依赖于后序遍历的顺序:左、右、根。具体细节看下面代码。

代码一:

/*递归1、最简单的算法*/
/*class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
        if(root){
            postorderTraversal(root->left);
            postorderTraversal(root->right);
            res.push_back(root->val);
        }
        return res;
    }
private:
    vector res;
};*/

代码二:

/*递归2*/
/*class Solution {
public:
     vector postorderTraversal(TreeNode *root) {
         vector res;
         stack res_;
         postorder(root, res_);
         while(!res_.empty()){
             res.push_back(res_.top());
             res_.pop();
         }
         return res;
     }
     void postorder(TreeNode *root, stack &res) {
         if (!root) 
             return;
         res.push(root->val);
         if (root->right) 
             postorder(root->right, res);
         if (root->left) 
             postorder(root->left, res);
     }
 };*/

代码三:

/*递归3*/
/*class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
         vector res;
         postorder(root, res);
         return res;
    }
    void postorder(TreeNode *root, vector &res) {
         if (!root) 
             return;
         if (root->left) 
             postorder(root->left, res);
         if (root->right) 
             postorder(root->right, res);
         res.push_back(root->val);
     }
};*/

代码四:

/*迭代1*/
/*class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
        vector res;
        vector res_;
        stack s;
        TreeNode *p = root;
        if(p == NULL)
            return res;
        while(p || !s.empty()){
            if(p != NULL){
                res.push_back(p->val);
                s.push(p->left);
                p = p->right;
            }
            else{
                p = s.top();
                s.pop();
            }
        }
        for(int i = res.size()-1;i >= 0;--i)
            res_.push_back(res[i]);
        return res_;
    }
};*/

代码五:

/*迭代2*/
/*class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
        stack s;
        vector res;
        if(root==NULL)
           return res;
        TreeNode* p = root;
        TreeNode* temp = NULL;
        while(p||!s.empty()){
            if(!p){
               p=s.top();//弹出栈内结点
               s.pop();
               if(p->right){//此节点有右子树,P转到该右子树,并把该结点的右子树置为NULL,押回
                  temp=p->right;
                  p->right=NULL;
                  s.push(p);
                  p=temp;
               }
               else{
                  res.push_back(p->val);
                  p=NULL;
               }
            }
            else{
                s.push(p);
                p=p->left;
            }
        }
        return res;
    }
};*/

代码六:

/*迭代3*/
/*class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
        vector res;
        stack s;
        TreeNode *p = root;
        while (!s.empty() || p) {
            if (p) {
                s.push(p);
                res.insert(res.begin(), p->val);    //反向添加,而前序是正向添加
                p = p->right;
            } 
            else {
                TreeNode *t = s.top(); 
                s.pop();
                p = t->left;
            }
        }
        return res;
    }
};*/

代码七:

/*迭代4*/
/*class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
        vector res;
        stack s;
        TreeNode *p = root;
        while(!s.empty() || p){
            if(p){
                s.push(p);
                p = p->left;
            }
            else{
                if(p->right== NULL ){
                    res.push_back(p->val);
                    s.pop();
                }    
                else
                    p = p->right;
            }    
        }
        return res;
    }
};*/

代码八:

/*迭代5*/
//常数时间空间复杂度
/*class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
        vector res;
        TreeNode *tmp = NULL;
        while(root){
            tmp=root->right;
            while(tmp && tmp->left!=NULL && tmp->left!=root){
                tmp = tmp->left;
            }
            if(tmp == NULL){
                res.push_back(root->val);
                root=root->left;
            }
            else if(tmp->left == NULL){
                res.push_back(root->val);
                tmp->left=root;
                root=root->right;
            }
            else{
                tmp->left=NULL;
                root=root->left;
            }
        }
        reverse(res.begin(),res.end());
        return res;
    }
};*/

代码九:

/*迭代6*/
/*class Solution {
public:
    vector postorderTraversal(TreeNode* root) {
        vector ans;        
        if(!root)            
            return ans;                
        stack st;        
        TreeNode *p = root;        
        st.push(p);        
        while(!st.empty()){            
            p = st.top();
            if(!p->left && !p->right){
                st.pop();
                ans.push_back(p->val);
            }
            if(p->right){
                st.push(p->right);
                p->right = NULL;
            }           
            if(p->left){
                st.push(p->left);
                p->left = NULL;
            }          
        }        
        return ans;
    }
};*/

你可能感兴趣的:(c++)