leetcode 144. Binary Tree Preorder Traversal ----- java
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?
/**
* 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) { Deque<TreeNode> queue = new ArrayDeque<TreeNode>(); List list = new ArrayList<Integer>(); if( root == null )
return list; queue.add(root); while( !queue.isEmpty() ){ TreeNode node = queue.poll();
list.add(node.val); if( node.right != null )
queue.addFirst(node.right);
if( node.left != null )
queue.addFirst(node.left); } return list; }
}
leetcode 144. Binary Tree Preorder Traversal ----- java的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- Leetcode 144. Binary Tree Preorder Traversal
参考例子:[8,3,1,6,4,7,10,14,13] 8,3,1 和 6,4 说明从root开始,沿着左臂向下寻找leaf 的过程中应该逐个将node.val push入ans. class Sol ...
- LeetCode 144. Binary Tree Preorder Traversal 动态演示
先序遍历的非递归办法,还是要用到一个stack class Solution { public: vector<int> preorderTraversal(TreeNode* root) ...
随机推荐
- MATLAB 函数
MATLAB函数大全 1.常见 http://wenku.baidu.com/link?url=tPpwD7Ox_1sG-SQv_XdYszBAPY9LX_Zb_dde_5JeOiu7RwN_i14X ...
- 在Tomcat下配置Solr 4.x 版本
solr是一款非常优秀的全文检索服务器,最新版本在配置和前台页面上都做了较大的改动, 所以对用惯了老版本的朋友们来说,再重新配置新版本的solr,无疑又是一件痛苦的事情. 配置环境:windows ...
- Android EditText email、数字验证
在做Android注册登录模块的时候,经常需要在客户端就验证用户输入的信息的正确性,如填写邮箱需要验证是否是邮箱,填写手机.年龄等信息需要验证是否是数字.先介绍一下验证邮箱的代码: /** * met ...
- iOS之沙盒机制和如何获取沙盒路径
iOS APP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URL Scheme.沙盒里面的文件可以是照片.声音文件. ...
- idea修改jsp后不会自动编译和替换(转)
使用IntelliJ IDEA开发JavaWeb项目时,修改了JSP后刷新浏览器无法及时显示修改后的页面? 解决办法:tomcat配置中,On frame deactivation属性选择Update ...
- 嵌套遍历<s:iterator>map=new TreeMap(string,Map(string,User))
//嵌套遍历,先给外层的map(假设是放在root中的,如果放在context的map中,要加#)取个别名,放到Actioncontext中 <s:iterator value="ma ...
- HTML--3css样式表
CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/ 此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控 ...
- Security Checklist (路由器安全checklist)
Security Checklist Website by Michael Horowitz Home | Introduction | Router Bugs | Security Che ...
- 利用DetachedCriteria实现模糊查询和分页
分类: Java-Developing 前段时间在做模糊查询,并利用数据库分页,DAO用hibernate实现,刚开始的时候 根据业务层的数据,拼hql语句进行查询,且不说要进行一些if判断,单 ...
- (转)iphone数据存储之-- Core Data的使用
原文:http://www.cnblogs.com/xiaodao/archive/2012/10/08/2715477.html iphone数据存储之-- Core Data的使用(一) 一. ...