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

解法一:递归法

/**
 * Definition for binary tree
 * 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;
        postOrder(root, ret);
        return ret;
    }
    void postOrder(TreeNode* root, vector<int>& ret)
    {
        if(root == NULL)
            return;
        postOrder(root->left, ret);
        postOrder(root->right, ret);
        ret.push_back(root->val);
    }
};

解法二:借助栈的非递归回溯解法。由于不能在树节点中增加visited成员,所以开辟map进行访问记录。

需要注意的是,出栈(即结束访问)有两种情况:

(1)左右节点均为空,即叶节点

(2)左右节点均已访问过

/**
 * Definition for binary tree
 * 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 == NULL)
            return ret;

        map<TreeNode*, bool> m; //visited
        stack<TreeNode*> s;
        s.push(root);
        m.insert(map<TreeNode*, bool>::value_type(root, true));
        while(!s.empty())
        {
            TreeNode* cur = s.top();

            while(cur->left)
            {
                TreeNode* left = cur->left;
                map<TreeNode*, bool>::iterator it = m.find(left);
                if(it == m.end())
                {//not visited
                    s.push(left);
                    m.insert(map<TreeNode*, bool>::value_type(left, true));
                    cur = left; //down left
                }
                else
                    break;  //cur remains
            }
            if(cur->right)
            {
                TreeNode* right = cur->right;
                map<TreeNode*, bool>::iterator it = m.find(right);
                if(it == m.end())
                {//not visited
                    s.push(right);
                    m.insert(map<TreeNode*, bool>::value_type(right, true));
                    cur = right;    //down right
                }
                else
                {//cur finish
                    ret.push_back(cur->val);
                    s.pop();
                }
            }
            else
            {//cur finish
                ret.push_back(cur->val);
                s.pop();
            }
        }
        return ret;
    }
};

解法三:在Discussion看到一种神一样的解法。

前序是:根左右

后续是:左右跟

因此可以将前序改为根右左,然后逆序为左右根输出。

前序遍历不需要回溯(对应图的深度遍历),是一种半层次遍历,因此效率很高。

/**
 * Definition for binary tree
 * 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 == NULL)
            return ret;

        stack<TreeNode*> s;
        s.push(root);
        while(!s.empty())
        {
            TreeNode* cur = s.top();
            s.pop();
            ret.push_back(cur->val);
            if(cur->left)
                s.push(cur->left);
            if(cur->right)
                s.push(cur->right);
        }
        reverse(ret.begin(), ret.end());
        return ret;
    }
};

时间: 2024-10-23 10:55:03

【LeetCode】Binary Tree Postorder Traversal (3 solutions)的相关文章

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

【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)复杂度分析:时