LintCode Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.
Given:
1
/ \
2 3
/ \
4 5
return [1,2,4,5,3].
Thinking:
For this problem, you need to think about using recursive or non-recursive methods. As recursive method, we should think the order of traverse is to visit the node itself then visit left and right subtrees for traverse. So use a helper method to pass the list into which to acomplish hte adding of numbers to hte list.
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
if (root == null) {
return result;
}
traverse(root,result);
return result;
}
public void traverse (TreeNode root,ArrayList<Integer> list) {
if (root == null) {
return;
}
list.add(root.val);
traverse(root.left,list);
traverse(root.right,list);
}
}
For non-recursive method, think about using a stack to push nodes in order of right first then left. Because this order will lead to right node is always below the left node. Meanwhile, before finishing the left node's children and decedents you will go deep until leaves.
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
// write your code here
// use stack to achieve pre-order
Stack<TreeNode> stack = new Stack<TreeNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
stack.push(root);
// eadge case
if (root == null) {
return result;
}
// while loop to traverse every node in tree
while (!stack.isEmpty()) {
TreeNode curr = stack.pop();
result.add(curr.val);
if (curr.right != null) {
stack.push(curr.right);
}
if (curr.left != null) {
stack.push(curr.left);
}
} return result;
} }
LintCode 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 ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 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 (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- 安装 phoneGap
npm config set registry http://registry.cnpmjs.org 使用最新版本PhoneGap ,决定使用官方提供的方法安装一次. 官方提供方法有些地方没有提到,因 ...
- CSS布局基础之二认识Viewport
什么是viewport viewport,等同于浏览器窗口. 功能:约束你网站中最顶级包含块(containing block)元素html标签. 什么是包含块(containing block)?下 ...
- Hibernate 中出现 XXXX is not mapped 问题
1.查询的不是数据库名是实体名…Entity 2.也许是你where条件后的字段名称写错了
- SQL Server显式事务与隐式事务
事务是单个的工作单元.如果某一事务成功,则在该事务中进行的所有数据修改均会提交,成为数据库中的永久组成部分.如果事务遇到错误且必须取消或回滚,则所有数据库修改均被清除. SQL Server中有一下几 ...
- drupal前端开发的第一点
就是把admin/config/development/performance下面的选项全部去掉,方便作为一个匿名用户来访问,不用每次都drush cc all.
- iOS中JS 与OC的交互(JavaScriptCore.framework)
iOS中实现js与oc的交互,目前网上也有不少流行的开源解决方案: 如:react native 当然一些轻量级的任务使用系统提供的UIWebView 以及JavaScriptCore.framewo ...
- Linux 忘记密码解决方法
很多朋友经常会忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码即可. 步骤如下: 重启linux系统 3 ...
- SPSS数据分析—非参数检验
统计学的假设检验可以分为参数检验和非参数检验,参数检验都是根据一些假设条件推算而来,当这些假设条件无法满足的时候,参数检验的效能会大打折扣,甚至出现错误的结果,而非参数检验通常是没有假设条件的,因此应 ...
- VUE应用的一些感受
方便,数据绑定太方便了. 一个组件一个.vue文件特别清晰. 讲真vue比angular好学多了. webpack打包最近看懂,通过一个主文件把require的文件都打进来.业务代码放build里,引 ...
- stash存储
比如我的fsm_ngh分支,被a修改了,可是我本地还有没有提交的东西,就不能切换分支也不能下拉,也不能直接上传,否则分支会混乱. 这个时候stash的优点来了,先把你修改的暂存隐藏起来,然后pull远 ...