LintCode Binary Tree Inorder Traversal
Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
Example
Given binary tree {1,#,2,3}
,
1
\
2
/
3
return [1,3,2]
.
For in-order traversal we all know that firstly we want to go to the lowest level and most left part to see the node and start from that node because the smallest value of that node in the whole tree. For this problem I have two methods implemented for the question. First one is the basic one which is the recursive method as below. This is pretty straight forward as shown in the description that for each node we traverse left subtree first then visit node then traverse right subtree.
1, The traverse of the node equals traverse left subtree and then visit node, and finally traverse the right subtree.
2, The left sub-tree and right sub-tree are having less problem size than the initial problem.
3, The base case is when the node is null or children of leaves because the recursive part will visit the node before traverse right sub-tree and after traverse of left sub-tree.
/**
* 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: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
traverse(result, root);
return result;
} public void traverse(ArrayList<Integer> list, TreeNode root) {
if (root == null) {
return ;
}
traverse(list,root.left);
list.add(root.val);
traverse(list,root.right);
} }
The second way of implementation is utilizing the stack to help to iteratively traverse the nodes.
The algorithm is:
Go to the most left as deep as possilble and push all the nodes on the way to the stack
List Result
TreeNode curr;
while
curr <---- pop() first element on the top of the stack;
add to Result /print the element poped from the stack;
while curr.right exist
curr <---- curr.right
while curr is not nulld
push curr to the stack
curr <----curr.left
return (Result)
/**
* 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: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode curr = root; if(curr == null) {
return result;
} while (curr.left != null) {
stack.push(curr);
curr = curr.left;
} stack.push(curr);
while (!stack.isEmpty()) {
curr= stack.pop();
result.add(curr.val);
if (curr.right != null) {
curr = curr.right;
while (curr.left !=null ) {
stack.push(curr);
curr=curr.left;
}
stack.push(curr);
}
}
return result;
}
}
LintCode Binary Tree Inorder Traversal的更多相关文章
- 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- 3月3日(4) Binary Tree Inorder Traversal
原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【LeetCode】Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...
随机推荐
- centos7 docker tomcat7
docker run --name=tomcat7_tmp -ti centos7/jdk7 /bin/bash cd /home wget http://apache.fayea.com/tomca ...
- day5 -指针
指针和指针变量 指针就是地址,地址就是指针 地址就是存放单元的编号 指针变量是存放地址的变量 指针和指针变量是两个不同的概念,但是要注意,通常我们叙述时会把指针变量简称为指针,实际他们含义并不一样 指 ...
- RedHat3.4安装GIT
1.首先到官网上下载git包,地址为http://git-scm.com/download 注意:选择下载Older releases 2.输入命令tar zxvf git-1.7.9.4.tat.g ...
- HBase之集群状态
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.had ...
- python内置函数 2
import__( name[, globals[, locals[, fromlist[, level]]]])被 import 语句调用的函数. 它的存在主要是为了你可以用另外一个有兼容接口的函数 ...
- hdu 3518 (后缀数组)
题目描述: 找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠). code: 后缀数组处理,对于得到height 进行查找... 参考http://blog.csdn.net/mis ...
- oracle中DDL DML指什么?
DDL create table 创建表 alter table 修改表 drop table 删除表 truncate table 删除表中所有行 create index 创建索引 drop in ...
- hdu 5877/ 2016 ACM/ICPC Dalian Online 1010 Weak Pair
题目链接 分析:树上的节点祖先与儿子的关系,一般就会想到dfs序.正解就是对树先进行dfs序排列,再将问题转化到树状数组统计个数.应该把节点按照权值从大到小排序,这样对于,就是从小到大的顺序.这样更新 ...
- 读javascript高级程序设计08-引用类型之Global、Math、String
一.Global 所有在全局作用域定义的属性和方法,都属于Global对象. 1.URI编码: encodeURI():主要用于对整个URI编码.它不会对本身属于URI的特殊字符进行编码. encod ...
- win7任务栏只显示日期不显示年月日
某一天突然发现笔记本任务栏上的时间显示只剩下了时间,没有了年月日 于是百度 搜到的结果大多是如何设置显示的格式 yyyy-MM-dd 继续搜 终于搜到了正确答案 结果令我瞠目结舌 着实无奈 是因为 ...