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

leetcode:Binary Tree Postorder Traversal

时间:2015-04-02 19:02:48      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:leetcode   c++   traversal   

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

标签:leetcode   c++   traversal   

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

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