<LeetCode OJ> 145. Binary Tree Postorder Traversal

在此之前回顾前序遍历和中序遍历:

1,前序遍历:

基本规则,总是先访问根节点在左节点,在右节点

递归解法:

class Solution {
public:
    vector<int> result;
    vector<int> preorderTraversal(TreeNode* root) {
        if(root){
            result.push_back(root->val);
            preorderTraversal(root->left);
            preorderTraversal(root->right);
        }
        return result;
    }
};

迭代解法:

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        if (root==NULL)
            return result;
        stack<TreeNode*> stk;
        TreeNode* pCurNode=root;
        stk.push(pCurNode);
        while (!stk.empty())
        {
            pCurNode = stk.top();
            stk.pop();
            result.push_back(pCurNode->val);  

            if (pCurNode->right)
                stk.push(pCurNode->right); //后压进来的后访问
            if (pCurNode->left)
                stk.push(pCurNode->left);
        }
        return result;
    }
private:
  vector<int> result;
};

2,中序遍历:

总是遍历树的左子,再当前节点,再右子

递归解法:

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
         if(root){
            inorderTraversal(root->left);
            result.push_back(root->val);
            inorderTraversal(root->right);
        }
        return result;
    }
private:
    vector<int> result;
};  

迭代解法:

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        if (root==NULL)
            return result;
        stack<TreeNode*> stk;
        TreeNode* pCurNode=root;
        while (!stk.empty() ||pCurNode)
        {
            while(pCurNode)//先把当前节点的所有左子压进去,
            {
                stk.push(pCurNode);
                pCurNode=pCurNode->left;
            }  

            pCurNode=stk.top();
            stk.pop();//访问当前节点
            result.push_back(pCurNode->val);  

            pCurNode=pCurNode->right;//再访问当前节点的右子树
        }
        return result;
    }
private:
    vector<int> result;
}; 

综上述:都可以采用栈来实现。将当前节点,左子,右子压进栈的顺序以及访问后的弹出栈形成的访问顺序,目的就是在维护要求的遍历顺序。

3,后序遍历:

总是先访问左子,再右子,最后当前节点

递归解法:

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        if(root!=NULL)
        {
            postorderTraversal(root->left);
            postorderTraversal(root->right);
            result.push_back(root->val);
        }
        return result;
    }
private:
    vector<int> result;
};

迭代解法

然而发现回顾了一下也没用,尼玛,尽然要用两个栈或者用到了前后节点来维护。

别人的算法设计:

输出栈:获取最终的输出结果

辅助栈:

1,利用辅助栈总是先将当前节点压进栈,接着弹给输出栈(此时他的顺序已确定,所以最先进输出栈,因为最后访问),

2,再左子进辅助栈,在右子进辅助栈(此时在栈顶,下次循环他已被确定顺序)

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        if (root==NULL)
            return result;
        stack<TreeNode*> outStk;//辅助栈将维护此栈的顺序就是后序遍历的顺序
        stack<TreeNode*> assistStk;//辅助栈
        TreeNode *pCurNode = root;
        assistStk.push(pCurNode);

        while (!assistStk.empty())
        {
            pCurNode = assistStk.top();
            assistStk.pop();
            outStk.push(pCurNode);

            if (pCurNode->left!=NULL)
                assistStk.push(pCurNode->left);
            if (pCurNode->right!=NULL)
                assistStk.push(pCurNode->right);
        }
        while (!outStk.empty())
        {
            result.push_back(outStk.top()->val);
            outStk.pop();
        }
        return result;
    }
private:
  vector<int> result;
};

注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50572826

原作者博客:http://blog.csdn.net/ebowtang

时间: 2024-12-21 00:17:03

<LeetCode OJ> 145. Binary Tree Postorder Traversal的相关文章

【leetcode】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

Java for LeetCode 145 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. 后序遍历,左子树→右子树→根节点 前序遍历的非递归实现需要一个计数器,方法是需要重写一个类继承TreeNode,翁慧玉教材<数据结构:题解与拓展>P113有详细介绍,这里略.递归JAVA实现如下: public Lis

145. Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. 解题思路: 方法一:递归方法... class Solution {public: vector<int> postorderTraversal(TreeNode* root) { vector<int>

145. Binary Tree Postorder Traversal (Stack, Tree)

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? class Solution { public: vector<int> postor

145. Binary Tree Postorder Traversal QuestionEditorial Solution

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 每次都先push right, 然后left. 但是需要一个sentinel存每次上一次p