Leet-code144. Binary Tree Preorder Traversal
这是一道将二叉树先序遍历,题目不难。
首先采用深搜递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public static List<Integer> resultlist = new ArrayList<Integer>(); public static void dfs (TreeNode root ,boolean flag ) {
if(flag==true)
{
resultlist.clear();
}
if(root==null)
{
return ;
} resultlist.add(root.val); if(root.left!=null)
{
dfs(root.left,false);
}
if(root.right!=null)
{
dfs(root.right,false);
} } public static List<Integer> preorderTraversal(TreeNode root ) {
dfs(root,true);
return resultlist;
} }
非递归实现方式
public static List<Integer> preorderTraversal(TreeNode root ) {
List<Integer> result = new ArrayList<>();
if(root == null)
return result;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.empty()){
TreeNode n = stack.pop();
result.add(n.val);
if(n.right != null){
stack.push(n.right);
}
if(n.left != null){
stack.push(n.left);
}
}
return result;
}
Leet-code144. Binary Tree Preorder Traversal的更多相关文章
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 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 (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
随机推荐
- => in Scala
What does => mean in Scala? 操作符=>在Scala中什么意思? 百度了下,有个论坛给出了比较全面的回答,请参见http://stackoverflow.com/ ...
- python2 + selenium + eclipse 中,配置好runserver 127.0.0.1:9000,运行的时候,报错
python2 + selenium + eclipse 中,配置好runserver 127.0.0.1:9000,运行的时候,报错,如图: 原因: google发现是WSGI appl ...
- python 进行web测试
1:安装nosetests Python 单元测试框架之Nose http://blog.sina.com.cn/s/blog_65a8ab5d0101fihb.html Python nose te ...
- Hadoop中Partition解析
1.解析Partition Map的结果,会通过partition分发到Reducer上,Reducer做完Reduce操作后,通过OutputFormat,进行输出,下面我们就来分析参与这个过程的类 ...
- Elasticsearch中提升大文件检索性能的一些总结
笔者在实际生产环境中经常遇到一些大文件的检索,例如一些书籍内容,PDF文件等.今天这篇博客主要来探讨下如何提升ES在检索大文件的一些性能,经验有限,算是一个小小的总结吧! 1.大文件是多大? E ...
- ACM-ICPC2018徐州网络赛 Hard to prepare(dp)
Hard to prepare 28.63% 1000ms 262144K After Incident, a feast is usually held in Hakurei Shrine. T ...
- WPF语言切换,国际化
winform语言切换在每个窗口下面有一个.resx结尾的资源文件,在上面添加新字符串就好了: WPF语言切换跟winform不一样的地方在于需要自己添加资源文件,并且这个资源文件可以写一个,也可以写 ...
- E20190419-hm
diagram n. 图表; 示意图; 图解; [数] 线图; contingency n. 意外事故,偶发事件; 可能性,偶然性; [审计] 意外开支; crash v. 碰撞; 使发出巨响; 暴跌 ...
- U3D shaderlab 相关指令开关
Subshader { [Tags] [CommonState] Passdef [Passdef ...] }Tags分为SubShader Tags和Pass Tags,Tags放在SubShad ...
- java 多线程下载文件并实时计算下载百分比(断点续传)
多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...