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

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

  2. 3月3日(4) Binary Tree Inorder Traversal

    原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...

  3. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  4. LeetCode: Binary Tree Inorder Traversal 解题报告

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  5. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  6. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  7. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

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

  9. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

随机推荐

  1. [转](二)unity4.6Ugui中文教程文档-------概要-UGUI Canvas

    大家好,我是孙广东.   转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unityma ...

  2. 特征的Attribute Only选项

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

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

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

  4. struts入门初步(一)

      struts2.0与struts1.0运用了不同的框架,有一定的不兼容性. struts2.0借鉴了webwork的框架思想. Struts2的基本步骤: 1.拷贝struts的jar到项目中(导 ...

  5. The import javax.servlet.http.HttpServletRequest cannot be resolved

    Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...

  6. python--分布式爬虫

    //server import socket, select, re, queue, redis from multiprocessing import Pool, cpu_count from py ...

  7. C#窗体自定义控件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  8. 读<jquery 权威指南>[1]-选择器及DOM操作

    今天是小年了,2013马上要过去了,但是学习不能间断啊.最近正在看<jQuery权威指南>,先温习一下选择器和DOM操作. 一.基本选择器 1.table单双行: $(function ( ...

  9. cookie的使用

    cookie的使用 1.创建cookie对象: function addCookie(name, value) { var path = filterRoot(); var cookieString ...

  10. varchar类型转换为numeric的值时有问题原因

    numeric的值不应该用单引号括起来...........