LeetCode 144. Binary Tree Preorder Traversal 动态演示

Posted leetcoder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 144. Binary Tree Preorder Traversal 动态演示相关的知识,希望对你有一定的参考价值。

先序遍历的非递归办法,还是要用到一个stack

 

class Solution 
public:
    vector<int> preorderTraversal(TreeNode* root) 
        vector<int> ret;
        if(!root)
            return ret;
        stack<TreeNode*> stk;
        stk.push(root);
        //ahd(root)
        //a(stk)
        //a(ret)
        while(stk.size()>0)
            TreeNode* cur=stk.top(); stk.pop();
            //a(cur)
            //lk("root", cur)
            ret.push_back(cur->val);
            //dsp            
            if(cur->right) stk.push(cur->right);
            if(cur->left) stk.push(cur->left);  
            //dsp
        
        
        return ret;
    
;

程序运行动态演示:http://simpledsp.com/FS/Html/lc144.html

 

以上是关于LeetCode 144. Binary Tree Preorder Traversal 动态演示的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 144. Binary Tree Preorder Traversal

[leetcode-144-Binary Tree Preorder Traversal]

LeetCode-144-Binary Tree Preorder Traversal

leetcode [144]Binary Tree Preorder Traversal

Java [Leetcode 144]Binary Tree Preorder Traversal

LeetCode 144: Binary Tree Preorder Traversal