LeetCode——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]
.
Note: Recursive solution is trivial, could you do it iteratively?
中文:二叉树的前序遍历(根-左-右)。
能用非递归实现吗?
递归:
public class BinaryTreePreorderTraversal {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if(root == null)
return list;
list.add(root.val);
list.addAll(preorderTraversal(root.left));
list.addAll(preorderTraversal(root.right));
return list;
}
// Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
}
非递归:先把右节点的值压入栈中,再压入左的。弹出左的,弹出右的……。
public List<Integer> preorderTraversal(TreeNode root){
List<Integer> list = new ArrayList<Integer>();
if(root == null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
list.add(node.val);
if(node.right != null)
stack.push(node.right);
if(node.left != null)
stack.push(node.left);
}
return list;
}
LeetCode——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 ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [leetcode]Binary Tree Preorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...
- LeetCode Binary Tree Preorder Traversal 先根遍历
题意:给一棵树,求其先根遍历的结果. 思路: (1)深搜法: /** * Definition for a binary tree node. * struct TreeNode { * int va ...
- 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)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
随机推荐
- mysql 修改表的字符集
修改表的字符集 88down voteaccepted If you want to change the table default character set and all character ...
- python学习笔记(16)--django的安装
说明: 1. 直接在cmd输入: pip install Django==1.10.6前提是安装了python,pip并添加了环境变量 2. http://www.lfd.uci.edu/~gohlk ...
- HTML5的video虽然可用controls来展示控件
HTML5的video虽然可用controls来展示控件,并进行控制播放暂停等,但是不同的浏览器显示的效果可能不一样,所以很多时候我们需要使用Dom来进行自定义的一些操作和控制.下面是一个小例子. 当 ...
- python content list(1--4)
part 1 python language 1. environment building and config 2. variable and data type 3. programming b ...
- 【WPF】设置ListBox容器Item的流式布局
需求:像下图那样显示把一组内容装入ListBox中显示.要求用WrapPanel横向布局,顺序如图中的数字. 问题:ListBox默认的布局是从上往下单列的,所以需要设置布局. <ListBox ...
- NFS根文件系统
按照以前文档可以正确制作根文件系统,并且开发板可正确nfs挂测主机目录. 现只需修改bootargs,使内核启动时挂测文件系统即可.setenv bootargs mem=64M console=tt ...
- 关于BETA、RC、ALPHA、Release、GA等版本号的意义
关于BETA.RC.ALPHA.Release.GA等版本号的意义 转载 2016年06月19日 00:04:00 2049 0 1 . 最近由于工作需要经常要去SVN上拉开源项目的源码,对项目 ...
- Hibernate- 动态实例查询
什么是动态实例查询: 就是将查询出的单一列的字段,重新封装成对象,如果不适用特殊方法,会返回Object对象数组. 01.搭建环境 02.动态实例查询 需要使用相应的构造方法: public Book ...
- Cracking the coding interview--Q3.1
题目 原文: Describe how you could use a single array to implement three stacks. 译文: 你如何只用一个数组实现三个栈? 解答 我 ...
- windows rails new demo时候出错Make sure that `gem install mysql2 -v '0.3.15'` succeeds before bundling.
rails new demo --database=mysql最后报错Gem files will remain installed in D:/BillFiles/rails_dev/Ruby193 ...