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 ...
随机推荐
- Linux Shell 文本处理工具集锦
本文将介绍Linux下使用Shell处理文本时最常用的工具:find.grep.xargs.sort.uniq.tr.cut.paste.wc.sed.awk:提供的例子和参数都是最常用和最为实用的: ...
- Linux下Ruby开发配置
以CentOS为例 安装ruby:yum install ruby 安装ruby devel:yum install ruby-devel,这个装上以后,就可以使用gem安装第三方模块了 安装gem, ...
- "旋转的风车"----windows(GDI)绘图
这正是秋季将尽, 冬季未到的时节. 黄土高坡上已滚起了漫天黄沙, 而这里却是万里晴空如练! 风, 丝丝入骨! 未央柳即将枯死的枝条,仍在挣扎. 街道两旁清一色的银杏树叶, 金灿耀眼. 耀的令人感动, ...
- c#中对rgb的使用
今天发现c#中没有和vb类似的函数RGB(), 后来发现可以通过Color类来实现 从R,G,B的值可以得到一个Color,如: Color c = Color.FromArgb(255,0,0); ...
- Ubuntu安装Vim编辑器
1 安装Vim编辑器 由于Ubuntu预安装的是tiny版本,就会导致我们在使用上的产生不便. 所以我们要安装vim的full版本. 1.1 卸载旧版vi sudo apt-get remove vi ...
- IntelliJ IDEA的快捷键
本人整理了一部分,如果有无效的,欢迎指出.
- SaltStack实战
SaltStack实战 #安装 安装注意几点 python-libs-2.6.6-64.el6.x86_64 conflicts with file from package python-2.6.6 ...
- [教程] 离线封装原版WIN7系统 100%纯净
raymond 发表于 2015-11-28 18:54:15 https://www.itsk.com/thread-360376-1-4.html 对于之前我用母盘封装的系统,纯粹是为了体积而折腾 ...
- The import javax.servlet.http.HttpServletRequest cannot be resolved
Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...
- js的常用api
JavaScript常用API总结 原创 2016-10-02 story JavaScript 下面是我整理的一些JavaScript常用的API清单. 目录 元素查找 class操作 节点操作 属 ...