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) ...
随机推荐
- tornado介绍
一.定义 tornado是一个异步非阻塞模型的服务器(tcp/http).web框架. 二.特性 1.高并发 原因:其一,网络事件循环部分根据操作系统选择最高效的,如Linux会是epoll: 其二, ...
- DropMaster
DropMaster 是4个原生 VCL 控件的集合,在 Delphi 和 C++Builder 中使用.虽然包含在 Delphi 和 C++Builder 中的 VCL 组件允许同一程序内窗口之间的 ...
- 【转】./configure && make && make install详解
在Linux中利用源码包安装软件最重要的就是要仔细阅读安装包当中的README INSTALL两个说明文件,这两个文件会清楚的告诉你如何可以正确的完成这个软件的安装! 我们都知道源 ...
- [转]理解WSRF之一 使用WS-ResourceProperties (整理自IBM网站)
理解 WSRF第1部分-使用 WS-ResourceProperties 本 教程是一个由 4 部分组成的系列文章中的第 1 部分,该系列介绍 WSRF(Web Services Resource ...
- mybatis写mapper文件注意事项(转)
原文链接:http://wksandy.iteye.com/blog/1443133 xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响 < < > & ...
- Linear Predictors
In this chapter we will study the family of linear predictors, one of the most useful families of hy ...
- java.util 集合框架集合
java的集合框架为程序提供了一种处理对象组的标准方式.设计了一系列标准泛型接口: ⑴Collection ()接口,扩展了Iterable接口,位于集合层次结构的顶部,因此所有的集合都实现Colle ...
- mysql存储过程笔记
http://blog.csdn.net/wangchao0605/article/details/5935988 基本语法 创建存储过程 create procedure sp_name()begi ...
- (spring-第7回【IoC基础篇】)BeanDefinition的载入与解析&&spring.schemas、spring.handlers的使用
报错信息:Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http: ...
- Ubuntu系统下运行Eclipse出现找不到jre的问题的解决方法
在Ubuntu的某些版本下,比如10.10,会出现以下奇怪问题: 1. 安装jdk 我下载的jdk是bin格式的,直接运行解压,得到一个文件夹. 这个文件夹作为jdk的安装目录,可以拷贝到任意目录. ...