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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- 史上最"恐怖"的12生肖图,绝对超猛
史上最“恐怖”的十二生肖图,绝对超猛!图片依次是:鼠 牛 虎 兔 龙 蛇 马 羊 猴 鸡 狗 猪!
- Winform开发框架之单据窗体生成(主从表,流水单号)
源码地址:https://github.com/GarsonZhang/GZFramework.ShareDemo 前言 1.在开始本节前请先重置代码为 chapter-03-start 懒人地址:h ...
- 特征创建:Reference Characteristic、Template
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- python collections defaultdict
class_counts = defaultdict(int) 一.关于defaultdict 在Python里面有一个模块collections,解释是数据类型容器模块.这里面有一个collect ...
- SPI总线(同步)
一.SPI总线简介 串行外围设备接口SPI(serial peripheral interface)总线技术是Motorola公司推出的一种同步串行接口.SPI 用 于CPU与各种外围器件进行全双工. ...
- 转:C++的重载(overload)与重写(override)
C++ override overload 的区别 override是指在不同作用域中,多个函数原型完全一样,而实现却不同的函数.在C++中,经常发生在类的继承中.当基类中的某个方法是virtual ...
- python3 字符串相关函数
python版本 3.5 #Author by Liguangbo#_*_ coding:utf-8 _*_str="i like study python, welcome to my p ...
- python语言技巧
一 在写之前 最好指定python的路径: #!/usr/bin/python python 在linux中需要添加编码方式:以免出现中文乱码 # -*- coding: UTF-8 –*- 二 ...
- eclise 部署web工程报 There are no resources that can be added or removed from the server.
该文章转自: http://blog.csdn.net/dw_java08/article/details/7789601 eclise 部署web工程报 There are no resources ...
- "编写高质量代码"一书笔记
总结 css架构结构 : base : 共用样式 common: 通用控件样式 page: 页面级样式 js 架构结构: base 位于三层最底层,职责一是封装不同浏 ...