【Binary Tree Post order Traversal】cpp

题目:

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 1:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
            vector<int> ret;
            if (!root) return ret;
            stack<TreeNode *> sta;
            sta.push(root);
            while ( !sta.empty() ){
                TreeNode *tmp = sta.top();
                sta.pop();
                if ( tmp->left || tmp->right ){
                    TreeNode *l = tmp->left, *r = tmp->right;
                    tmp->left = tmp->right = NULL;
                    sta.push(tmp);
                    if (r) sta.push(r);
                    if (l) sta.push(l);
                }
                else{
                    ret.push_back(tmp->val);
                }
            }
            return ret;
    }
};

stack 2:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
            vector<int> ret;
            stack<TreeNode *> sta;
            TreeNode *curr = root;
            while ( !sta.empty() || curr )
            {
                if (curr)
                {
                    sta.push(curr);
                    curr = curr->left;
                }
                else
                {
                    curr = sta.top();
                    if ( !curr->right )
                    {
                        ret.push_back(curr->val);
                        sta.pop();
                        curr = NULL;
                    }
                    else
                    {
                        curr = curr->right;
                        sta.top()->right = NULL;
                    }
                }
            }
            return ret;
    }
};

tips:

上述两个代码都是基于stack的操作完成的后序遍历二叉树。

个人更喜欢stack 1的风格,思路如下:

0. 先压root入栈

1. 栈顶元素出栈

2. 如果其左右都为空:则可以直接推入ret中

否则:先将这个节点的left和right保存下来;再将这个节点与其子分支剪断(right left都置为NULL);再按照tmp, right, left的顺序入栈。

循环1~2,直到栈空,则后序遍历完成

网上一些答案很多都是基于stack 2这种方法,维护一个当前指针curr。

这个思路就是一条道走到黑的思路(DFS深搜)

1. curr不为NULL,则一直沿着left的方向走,直到走到NULL

2. 只要curr为NULL,则一定是栈顶元素的left已经没有了(走到头了),则需要判断栈顶元素的right是否为NULL;

  如果为NULL,则证明栈顶元素的left和right都访问过了,栈顶元素的val可以推入ret;

  如果不为NULL,则证明其right还得遍历。这个时候,需要完成两件事情:

    a. curr向right走

    b. 栈顶元素的right置为空(标记再次访问栈顶元素,其right已经再curr= curr->right的带领下处理过了)

其实stack 2的思路跟stack 1类似,都是需要判断栈顶元素的left和right是否都NULL,再决定栈顶元素的val是否推入ret。

时间: 2024-12-28 20:24:40

【Binary Tree Post order Traversal】cpp的相关文章

【Binary Tree Level Order Traversal】cpp

题目: 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] ] 代码

【Binary Tree Level Order Traversal II 】cpp

题目: 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 tr

【Binary Tree Right Side View 】cpp

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3,

【Binary Tree Maximum Path Sum】cpp

题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode

【LeetCode】107. Binary Tree Level Order Traversal II 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51508308 Subject 出处:https://leetcode.com/problems/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 rig

【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 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 traver

【leetcode】Binary Tree Level Order Traversal I &amp; II

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] ] 分层遍历二叉