[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 ...
随机推荐
- Hibernate实体映射转换列值
@Column(name="ADDTIME", insertable=false, updatable=false)@ColumnTransformer(read="CA ...
- python单线程下实现多个socket并发
先看服务端的代码 import sys # import socket import time import gevent from gevent import socket from gevent ...
- Message: u'$ is not defined' ; Stacktrace
status.html <html> <head> <meta http-equiv="content-type" content="tex ...
- win2003上传文件限制
Windows2003系统下,上传较大的文件时,会出现“Request 对象 错误 'ASP 0104 : 80004005'”错误或者出现空白页面的时候,此时,不要轻易的去找程序的问题,有可能是wi ...
- 移动端bug之解决方式
1.Android中元素被点击时产生的边框: * { -webkit-tap-highlight-color: rgba(250,250,250,0); /*更改点击事件的焦点色*/} 2.去除移 ...
- oracle的分析函数over(Partition by...) 及开窗函数
over(Partition by...) 一个超级牛皮的ORACLE特有函数. oracle的分析函数over 及开窗函数一:分析函数overOracle从8.1.6开始提供分析函数,分析函 ...
- zabbix分布式系统监视
http://blog.chinaunix.net/uid-25266990-id-3380929.html
- 2.自己搭建的一个简易的ioc容器
1.persondao类namespace MyselfIoC{ public class PersonDao { public override string ToStri ...
- winsock select 学习代码(2)
之前文章的改进版 服务器仅仅接受客户端发送的字符串并显示 客户端可以调节发送数目 但是不能超过64 // SelectServer.cpp : 定义控制台应用程序的入口点. // #include & ...
- zeromq学习记录(二)天气更新服务器使用ZMQ_SUB ZMQ_PUB
/************************************************************** 技术博客 http://www.cnblogs.com/itdef/ ...