Binary Tree Preorder Traversal
来源:互联网 发布:远程数据采集系统 编辑:程序博客网 时间:2024/06/10 17:41
class Solution {public: vector<int> preorderTraversal(TreeNode* root) { vector<int> result; stack<TreeNode*> tmp; TreeNode* pop_stack; if(root ==NULL) return result; tmp.push(root); while(!tmp.empty()) { pop_stack = tmp.top(); tmp.pop(); result.push_back(pop_stack->val); if(pop_stack->right != NULL) tmp.push(pop_stack->right); if(pop_stack->left != NULL) tmp.push(pop_stack->left); } return result; }};
需要注意问题:
1. stack的几种操作:
(转自:http://blog.csdn.net/morewindows/article/details/6950881)
注意:
1. push是在栈顶增加元素
2. pop只弹出元素,并不返回元素;想要获得栈顶元素,需要使用top()函数。
2. 注意入栈顺序~~
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
- [UVA11059]Maximum Product[暴力]
- 委托默认参数
- 海量存贮设计一
- angular 与video标签
- java 让开发自动化: 用 Eclipse 插件提高代码质量
- Binary Tree Preorder Traversal
- Android Studio 导出未签名 apk
- cocos2dx的那些坑
- 关于cron和crond,crontab以及bash脚本的写法等知识 cron的语法用法
- 代码 创建按钮
- GLib-GObject-WARNING **: Two different plugins tried to register
- 使用python获取实时卫星云图
- linux程序设计——数据报(第十五章)
- POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)