144. 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].
代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
if(root==null)
return list; list.add(root.val); if(root.left!=null)
list.addAll(preorderTraversal(root.left));
if(root.right!=null)
list.addAll(preorderTraversal(root.right)); return list;
}
}
144. Binary Tree Preorder Traversal的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
		
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
 - 二叉树前序、中序、后序非递归遍历 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 (3 solutions)
		
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
 - [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
		
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. For example:Given binar ...
 - (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
		
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
 
随机推荐
- 转: HTML的电子邮件链接标签mailto用法详解
			
mailto是网页设计制作中的一个非常实用的html标签,许多拥有个人网页的朋友都喜欢在网站的醒目位置处写上自己的电子邮件地址,这样网页浏览者一旦用鼠标单击一下由mailto组成的超级连接后,就能自动 ...
 - elasticsearch nested查询
			
项目里可能会遇到多级嵌套的情况,实际上最多两级,三级及以上,我测试不通过. 一级索引时,我插入数据,会自动创建索引映射:然二级时,索引映射必须手动创建. 映射: PUT test999 { " ...
 - 数据结构-Stack和Queue
			
实现: #include "c2_list.h" template <typename object> class Stack{ public: bool isEmpt ...
 - Android打开新的Activity并同时关闭当前Activity
			
Intent it = new Intent(); it.setClass(EditActivity.this, MainActivity.class); it.setFlags(Intent.FLA ...
 - C#根据当前日期获取星期和阴历日期
			
private string GetWeek(int dayOfWeek) { string returnWeek = ""; switch (dayOfWeek) { case ...
 - 父元素与子元素之间的margin-top问题(css hack)(转载)
			
情况: 父元素的盒子包含一个子元素盒子,给子元素盒子一个垂直外边距margin-top,父元素盒子也会往下走margin-top的值,而子元素和父元素的边距则没有发生变化. 解决方法: 1.修改父元素 ...
 - linux下一键安装 powershell,的bash脚本
			
说明 目前,linux下的powershell约等于pash.希望大家专注mono,关注pash. 一键安装脚本包括for centos6,centos7,ubuntu 14.04 ubuntu 1 ...
 - C/C++中函数参数传递详解(二)
			
昨天看了内存管理的有关内容,有一点了解,但不是很深入,发现之前写代码时有很多细节问题没有注意到,只知道这样做可以实现功能,却不知道为什么可以这样,对于采用自己的方法造成的隐患也未知,更不晓得还有其他方 ...
 - matlab代码 图像处理源码
			
非常不错的找图像处理源码的地方,源码搜搜. http://www.codesoso.com/Category.aspx?CategoryId=56
 - jQuery 关于 end() 方法的详细解释
			
<ul class="first"> <li class="foo">list item 1</li> <li> ...