码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】Binary Tree Postorder Traversal (3 solutions)

时间:2014-11-14 17:41:15      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   

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);
    }
};

bubuko.com,布布扣

 

解法二:借助栈的非递归回溯解法。由于不能在树节点中增加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;
    }
};

bubuko.com,布布扣

 

解法三:在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;
    }
};

bubuko.com,布布扣

【LeetCode】Binary Tree Postorder Traversal (3 solutions)

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/ganganloveu/p/4097550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!