首页 > 其他 > 详细

leetcode:Binary Tree Postorder Traversal

时间:2015-04-02 19:00:26      阅读:149      评论:0      收藏:0      [点我收藏+]
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> res;
        stack<pair<TreeNode *,int>>s;
        TreeNode *p = root;
        while(p!=NULL||!s.empty())
        {
            while(p)
            {
                s.push(pair<TreeNode*,int>(p,1));
                p = p->left;
            }
            pair<TreeNode*,int> q = s.top();
            s.pop();
            if(q.second==1)
            {
                s.push(pair<TreeNode*,int>(q.first,2));
                p = q.first->right;
            }
            else
            {
                res.push_back(q.first->val);
                p = NULL;
            }
        }
        return res;
    }
};

leetcode:Binary Tree Postorder Traversal

原文:http://blog.csdn.net/majing19921103/article/details/44833917

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!