[Tree]Binary Tree Preorder Traversal
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?
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
TreeNode* p = root;
if(p==NULL){
return res;
}
stack<TreeNode*> stk;
while(p || !stk.empty()){
while(p){
res.push_back(p->val);
stk.push(p);
p=p->left;
}
if(!stk.empty()){
p = stk.top()->right;
stk.pop();
}
}
return res;
}
};
[Tree]Binary Tree Preorder Traversal的更多相关文章
- LC 971. Flip Binary Tree To Match Preorder Traversal
Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this b ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- LeetCode & tree & binary tree
LeetCode & tree & binary tree 树 & 二叉树 refs https://leetcode.com/problemset/all/?topicSlu ...
- [Swift]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal
Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this b ...
- LeetCode 971. Flip Binary Tree To Match Preorder Traversal
原题链接在这里:https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/ 题目: Given a bina ...
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
- [Tree]Binary Tree Inorder Traversal
Total Accepted: 98729 Total Submissions: 261539 Difficulty: Medium Given a binary tree, return the i ...
- CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree
Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一 ...
- LeetCode_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)
Lowest Common Ancestor of a Binary Search Tree 一.题目描写叙述 二.思路及代码 二叉搜索树有个性质:左子树的值都比根节点小,右子树的值比根节点大.那么我 ...
随机推荐
- 四个常用.NET的SqlHelper的方法
至于我为什么要写这篇文章,也许很多人觉得网上大把的sqlhelper的封装类,的确,网上是有很多,我也看过网上很多的版本,但是我发现大多数都是代码生成器生成的,比如动软.CodeSmith等生成的,其 ...
- Oracle11g客户端安装及plsql配置
1,项目使用的是oracle11g数据库,安装个客户端访问服务器. 到oracle官方下载: http://www.oracle.com/technology/global/cn/software/t ...
- LInux系统及其文件系统
Linux系统:Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协 ...
- [Math]Sqrt(x)
Total Accepted: 75767 Total Submissions: 314003 Difficulty: Medium Implement int sqrt(int x). Comput ...
- GDAL显示线性shp文件
http://pan.baidu.com/s/1qWIDphU (工程文件在vs2008中编写) 1.使用到的技术 GDAL:读取矢量数据 GDI: 绘制矢量数据 2.详细解释 GDI绘图: ...
- Eclipse运行Tomcat7源码
1. 各环境版本: jdk1.6.0_45 (亲测jdk1.7.0_07会有问题,不要用1.7版本的) apache-ant-1.9.4 apache-tomcat-7.0.61-src 2. 安装a ...
- phonegap退出android程序
最近用android做了一个程序,在点“后退”的时候,会不停地后退,感觉不好. 查了些资料有这么些: 一.toast_plugin插件 <script type="text/javas ...
- Sublime Text2 快捷键设置
设置Tab两个空格: 点击 Preference -> Settings-User "tab_size":2, "translate_tabs_to_spaces& ...
- 【应用】Markdown 在线阅读器
前言 一款在线的 Markdown 阅读器,主要用来展示 Markdown 内容.支持 HTML 导出,同时可以方便的添加扩展功能.在这个阅读器的基础又做了一款在线 Github Pages 页面生成 ...
- 国内好用的公用DNS 服务器。
阿里 AliDNS 223.5.5.5 223.6.6.6 CNNIC SDNS 1.2.4.8 210.2.4.8 Google DNS 8.8.8.8 8.8.4.4 OpenDNS 208.67 ...