[LeetCode 题解]: Binary Tree Preorder Traversal
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
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?
2. 题意
先序遍历二叉树,递归的思路是普通的,能否用迭代呢?
3. 思路
非递归思路:<借助stack>
vector<int> preorderTraversal(TreeNode *root) {
stack<TreeNode* > st;
vector<int> vi;
vi.clear();
if(!root) return vi;
st.push(root);
while(!st.empty()){
TreeNode *tmp = st.top();
vi.push_back(tmp->val);
st.pop();
if(tmp->right) st.push(tmp->right);
if(tmp->left) st.push(tmp->left);
}
return vi;
}
递归思路:
class Solution {
private:
vector<int> vi;
public:
vector<int> preorderTraversal(TreeNode *root) {
vi.clear();
if(!root) return vi;
preorder(root);return vi;
}
void preorder(TreeNode* root){
if(!root) return;
vi.push_back(root->val);
preorder(root->left);
preorder(root->right);
}
};
4.相关题目
(1)二叉树的中序遍历:
(2)二叉树的后序遍历:
(3) 二叉树系列文章:
![]() |
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3896010.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解]: Binary Tree Preorder Traversal的更多相关文章
- leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java for LeetCode 144 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java [Leetcode 144]Binary Tree Preorder Traversal
题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
随机推荐
- Ubuntu下Eclipse热键Ctrl+Alt+Up无效的解决
原文链接 :http://rox.iteye.com/blog/875078 现在好多链接都打不开了, Ubuntu下一直用NetBeans开发,改了热键为Eclipse的,复制行不管用,一直认为是N ...
- moco入门
前提:moco是什么?有什么用 Moco是针对HTTP集成而生的,不过,现在也有人把它用在其它需要一个模拟服务器的场景中.比如,在移动开发中,有人开发一个移动应用,需要有一个远端服务,但在开发时,这个 ...
- ubuntu部署jenkins
https://www.cnblogs.com/lozz/p/9962316.html 1.安装 wget -q -O - https://pkg.jenkins.io/debian/jenkins- ...
- Easyui-datagrid显示时间的格式化代码
{field: 'Time', title: '时间', formatter: function (value, row, index) { var date = new Date(value); v ...
- Spring/AOP框架, 以及使用注解
1, 使用代理增加日志, 也是基于最原始的办法 import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; ...
- Javascript读写CSS属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Intellij IDEA 热部署处理
1. 首先参考IDEA热部署同行经验分享: Intellij IDEA 4种配置热部署的方法 2. IDEA 热部署实战: springboot项目: 不要引入热部署工具包spring-boot- ...
- 从文件中读取字符-多次调用read characters from file multiple calls
[抄题]: 接口:int read4(char * buf)一次从文件中读取 4 个字符.返回值是实际读取的字符数. 例如,如果文件中只剩下 3 个字符,则返回 3.通过使用read4 接口,实现从文 ...
- [leetcode]174. Dungeon Game地牢游戏
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 原生JS获取url汇总
在WEB开发中,许多开发者都比较喜欢使用javascript来获取当前url网址,本文就此为大家总结一下比较常用获取URL的javascript实现代码 URL即统一资源定位符 (Uniform Re ...
