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的更多相关文章

  1. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  2. 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日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

  4. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  5. 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 ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  8. LeetCode: Binary Tree Preorder Traversal 解题报告

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. 标准DSO设置

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  2. Android TextView自动换行文字排版参差不齐的原因

    今天项目没什么进展,公司后台出问题了.看了下刚刚学习Android时的笔记,发现TextView会自动换行,而且排版文字参差不齐.查了下资料,总结原因如下: 1.半角字符与全角字符混乱所致:这种情况一 ...

  3. memcache内存估算整理

    参考文章: http://blog.csdn.net/tonyxf121/article/details/7906428 http://zhihuzeye.com/archives/2361 memc ...

  4. hdu3847Trash Removal(凸包)

    链接 这题居然是WF的题, 应属于签到题.. 求一个多边形是否能被一个宽为d的矩形框住. 可以求一下凸包,然后枚举每条凸包的边,找出距离最远的点. #include <iostream> ...

  5. GeoHash

    查找是我们经常会碰到的问题,以前我做过一个这样的算法,在有序的数列(80万条左右),这批数据是根据维度由小到大排序的,寻找已知数据的位置,并且所相应的运算,由于这个算法要在嵌入式系统中做,如果一次在内 ...

  6. postgresql - 服务配置

    1.查看配置的修改是否需要重启数据库 select name,context from pg_settings where name like 'wal_buffers'; 如果context的值是p ...

  7. android 导入自己的生成的jar,老是 could not find class

    最近开始学习android,开发一个小项目,功能很简单,就是从服务器上获取数据,之后显示在手机上.打算把访问服务器的功能打包成一个jar文件.然后android 引入jar包. 在eclipse 里 ...

  8. C# 字符串转义和反转义

    System.Text.RegularExpressions.Regex.Unescape(s) System.Text.RegularExpressions.Regex.Escape(s)

  9. Js获取后台集合List的值并操作html

    功能:将后台传到前端JSP的List中的float型数值转换为百分比显示 HTML代码: <s:iterator value="colorConfigList" status ...

  10. eclipse 合并分支

    1.在要合并的分支项目中右击项目->team->merge 2.选择branch,将branch合并到brach2 3.出现冲突,查看http://www.cnblogs.com/jinT ...