Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree [1,null,2,3],

   1
\
2
/
3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

求二叉树的中序遍历,要求不是用递归。

先用递归做一下,很简单。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
List result = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if( root == null)
return result;
getResult(root);
return result;
} public void getResult(TreeNode root){
if( root.left != null)
getResult(root.left);
result.add(root.val);
if( root.right != null)
getResult(root.right);
} }

不用递归,用栈实现也是很简单的。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution { List result = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if( root == null)
return result;
Stack stack = new Stack<TreeNode>();
TreeNode node = root;
while( true ){
if( node.left == null){
result.add(node.val);
if( node.right == null ){
if( stack.isEmpty() )
break;
else
node = (TreeNode) stack.pop();
}else{
node = node.right;
} }else{
TreeNode flag = node;
node = node.left;
flag.left = null;
stack.push(flag); }
} return result;
} }

leetcode 94 Binary Tree Inorder Traversal ----- java的更多相关文章

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

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

  2. 49. leetcode 94. Binary Tree Inorder Traversal

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

  3. Java [Leetcode 94]Binary Tree Inorder Traversal

    题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...

  4. Leetcode 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  5. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  6. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

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

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  8. leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  9. 94. Binary Tree Inorder Traversal (Java)

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

随机推荐

  1. Js中 关于top、clientTop、scrollTop、offsetTop的用法

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  2. 大学生成绩管理系统(C语言)

    功能:成绩管理系统包含了学生的全部信息,每个学生是一个记录,包括学号,姓名,性别,班级,各科成绩(语数外). 系统功能: 1.信息录入——录入学生信息: 2.信息输出——显示所有信息: 3.信息查询— ...

  3. redis2.8--主从机同步流程

  4. redis2.8--c/s架构流程

  5. 详解C++中指针(*)、取地址(&)、解引用(*)与引用(&)的区别 (完整代码)

    一.初步了解--指针与取地址 先看程序: #include<cstdio> int main(void) { int num = 7; int *p = &num; printf( ...

  6. Controller方法的返回值

    方法的返回值1.ModelAndView这个就不多说,这是最基础的,前面定义一个ModelAndView,中途使用addObject方法添加属性,再返回.视图解析器会自动扫描到的.2.String这个 ...

  7. (三)获取iphone的IMSI

    今天的任务是 iPhone上怎样获取 imsi 信息 来判断所属运营商,资料找了很久!总体有两种方案,但是其中一种好像不行 这里我都记录下来吧: 1: 这是使用coreTelephony.framew ...

  8. 国内android帮助文档镜像网站---http://wear.techbrood.com/develop/index.html

    http://wear.techbrood.com/develop/index.html

  9. oracle中的cluster表

    大家对通常oracle中的cluster的理解是不准确的,经常和sql server中的cluster index混淆.Cluster是存储一组table的一种方法,这些table共享同一数据块中的某 ...

  10. WebGrid Enterprise免费下载

    WebGrid.NET Enterprise是一个为ASP.NET平台下WEB开发而设计的高级数据表格控件.WebGrid.NET为复杂的分层次导航交互式企业级信息传输提供了全面而先进的功能,它允许用 ...