【leetcode】Binary Tree Postorder Traversal (hard) ☆

二叉树的后序遍历

用标记右子树vector的方法

vector<int> postorderTraversal(TreeNode *root) {
        vector<int> ans;
        vector<TreeNode *> stack;
        vector<bool> isRight;
        stack.push_back(root);
        isRight.push_back(false);
        TreeNode * pNode = NULL;
        while(!stack.empty())
        {
            while((pNode = stack.back()) != NULL)
            {
                pNode = pNode->left;
                stack.push_back(pNode);
                isRight.push_back(false);
            }

            while(isRight.back() == true)
            {
                stack.pop_back();
                isRight.pop_back();
                ans.push_back(stack.back()->val);
            }
            stack.pop_back();
            isRight.pop_back();

            if(!stack.empty())
            {
                pNode = stack.back();
                stack.push_back(pNode->right);
                isRight.push_back(true);
            }
        }

        return ans;
    }

仅用一个栈的方法https://oj.leetcode.com/discuss/14118/post-order-traversal-using-two-satcks

    vector<int> postorderTraversal(TreeNode *root) {
        stack<TreeNode *> st;
        vector<int> vRet;
        TreeNode *p, *pre = root;

        if (!root) return vRet;
        p = root->left;
        st.push(root);
        while (!st.empty() )
        {
            while (p) { st.push(p); p = p->left; }
            p = st.top();
            while (!p->right || pre==p->right)
            {
                vRet.push_back(p->val);
                pre = p;
                st.pop();
                if (st.empty() ) break;
                p = st.top();
            }
            p = p->right;
        }
        return vRet;
    }
时间: 2024-10-13 15:56:34

【leetcode】Binary Tree Postorder Traversal (hard) ☆的相关文章

【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? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【LeetCode】Binary Tree Postorder Traversal (3 solutions)

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? 解法一:递归法 /** *

【LeetCode】Binary Tree Inorder Traversal (2 solutions)

Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归 /** * Defi

【LeetCode】Binary Tree Preorder Traversal (2 solutions)

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归 /** * De

【leetcode】Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3},   1   \   2  /3return [1,2,3]. 可用递归,进行.也可以用迭代. C++: 1 class Solution { 2 public: 3 void preorder(TreeNode * root, vector<int> &pat

【leetcode】Binary Tree Inorder Traversal

与前面的先序遍历相似. 此题为后序遍历. C++: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<i

【Leetcode】Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路:使用

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

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) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时