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

leetcode:Binary Tree Preorder Traversal

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

标签:c++   leetcode   

class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> res;
        stack<TreeNode *> s;
        TreeNode * p = root;
        while(p!=NULL||!s.empty())
        {
            while(p!=NULL)
            {
                res.push_back(p->val);
                s.push(p);
                p = p->left;
            }
            if(!s.empty())
            {
                p = s.top();
                s.pop();
                p = p->right;
            }
        }
        return res;
    }
};

leetcode:Binary Tree Preorder Traversal

标签:c++   leetcode   

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

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