144 Binary Tree Preorder Traversal 二叉树的前序遍历
给定一棵二叉树,返回其节点值的前序遍历。
例如:
给定二叉树[1,null,2,3],
   1
    \
     2
    /
   3
返回 [1,2,3]。
注意: 递归方法很简单,你可以使用迭代方法来解决吗?
详见:https://leetcode.com/problems/binary-tree-preorder-traversal/description/
Java实现:
递归实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
if(root==null){
return res;
}
return preorderTraversal(root,res);
}
private List<Integer> preorderTraversal(TreeNode root,List<Integer> res){
if(root==null){
return res;
}
res.add(root.val);
preorderTraversal(root.left,res);
preorderTraversal(root.right,res);
return res;
}
}
非递归实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
if(root==null){
return res;
}
Stack<TreeNode> stk=new Stack<TreeNode>();
stk.push(root);
while(!stk.isEmpty()){
root=stk.pop();
res.add(root.val);
if(root.right!=null){
stk.push(root.right);
}
if(root.left!=null){
stk.push(root.left);
}
}
return res;
}
}
144 Binary Tree Preorder Traversal 二叉树的前序遍历的更多相关文章
- LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
		
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
 - C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
		
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
 - lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
		
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
 - 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
		
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
 - 144	Binary Tree Preorder Traversal(二叉树先序遍历Medium)
		
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
 - Leetcode144. Binary Tree Preorder Traversal二叉树的前序遍历
		
给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class S ...
 - LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
		
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
 - [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
		
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
 - 144. Binary Tree Preorder Traversal (二叉树前序遍历)
		
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
 
随机推荐
- QT实现FTP服务器(三)
			
QFtpClient类的实现: #include "QFtpClient.h" #include <QDebug> #include <QThread> # ...
 - ruby hash排序
			
参考文章:http://blog.csdn.net/ppp8300885/article/details/49933305 a={a:1,b:20,c:3,d:0,e:7}逆序 a.sort{|k,v ...
 - 控制cms:CMSDropDownList的width
			
查找了一下kentico内部使用相关控件的代码,发现有2种方式,可以达到效果. 在我们自己的css class定义的地方,在class前面加上.cms-bootstrap .cms-bootstrap ...
 - codeforces   437B. The Child and Set  解题报告
			
题目链接:http://codeforces.com/contest/437/problem/B 题目意思:给出两个整数 sum 和 limit,问能否从1 - limit 这些数中选出一些数(注意: ...
 - 设置Tomcat的jvm内存问题
			
tomcat的jvm大小设置与操作系统以及jdk有关:具体来说: 1.操作系统是32bit的,程序最大内存访问空间是4G, 2的32次方,这是硬件决定的,跟windows linux没有任何关系. 2 ...
 - perl字符集处理
			
本文内容适用于perl 5.8及其以上版本. perl internal form 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种u ...
 - org.apache.hadoop.hbase.NotServingRegionException: Region is not online 错误
			
当遇到如下错误的时候 可能以为是regionserver 挂掉或者其他原因导致连接不上regionserver 但后面提示了Hbase 表statistic_login 具体信息 Thu Jan 1 ...
 - POJ2142:The Balance (欧几里得+不等式)
			
Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. F ...
 - Ski Course Design
			
链接 分析:读题!读题!读题!重要的事说三遍,中文翻译漏掉了一个重要的地方,每个只能用一次,调了一下午还以为标程错了,其实就是找一段长为17的区间,然后使所有都处于这个区间中代价最小,暴力枚举即可. ...
 - Power Crisis
			
传送门 这道题被严重恶意评分了,实际应该是绿题. 因为其实我们只需要模拟即可.这里我们引入一种新的东西:约瑟夫环. 它能直接告诉你约瑟夫问题中最后一个存活下来的人是谁.(不过下标是从0开始的,实际使用 ...