Binary Tree Preorder Traversal
来源:互联网 发布:ios远程访问mac 编辑:程序博客网 时间:2024/06/04 18:26
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
前序遍历,同样不能使用递归
class Solution {public: vector<int> preorderTraversal(TreeNode *root) { vector<int> rel; if(root==NULL) return rel; if(root) s.push(root); while(!s.empty()){ TreeNode * p = s.top(); s.pop(); while(p!=NULL){ rel.push_back(p->val); if(p->right!=NULL) s.push(p->right); p = p->left; } } return rel; } stack<TreeNode*> s;};
0 0
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- configure: error: Package requirements (glib-2.0 >= 2.10.1 gmodule-2.0 gthread-2.0) were not met:
- 发的抚摸对方浪费吗都看什么塞德里克塞德里克是
- Emoji表情符号录入MySQL数据库报错的解决方案
- python资源库
- char*,const char*和string
- Binary Tree Preorder Traversal
- Java回调函数理解
- 关于Android studio 中command line too long的解决方案
- 同步与异步IO、阻塞与非阻塞IO
- 项目中遇到的问题及错误整理
- Mac使用--截屏方法
- 搭建SNS游戏服务器
- HTML DOM_getElementById
- java并发编程教程