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. 【NOIP2015】提高组D1 解题报告

    P1978神奇的幻方 Accepted 描述 幻方是一种很神奇的 N ∗ N 矩阵:它由数字 1,2,3, … … , N ∗ N 构成,且每行.每列及两条对角线上的数字之和都相同. 当 N 为奇数时 ...

  2. HDU 5439 Aggregated Counting

    题目大意: 由1开始不断往数组中添加数 就是按照当前所在位置所在的数表示的个数添加这个数目的数 1 2 2 3 3 后面因为要填4,而4号位置为3,说明之后要填3个4 问题就是给定一个n,找到n出现的 ...

  3. Section 1.4 Packing Rectangles

    本来是USACO Training的1.4.1的,但是介于今早过了食物链想起了这道题实在是太怨念了,翻出自己写的AC程序居然有5KB!! 思路很简单,枚举,而且就图中的六种情况.但是第六种变化状况太多 ...

  4. powershell玩转xml之20问

    powershell玩转xml之20问 powershell 传教士 原创文章 2014-01-30,2015-10-27改 允许转载,但必须保留名字和出处,否则追究法律责任 问:xml文件编码情况如 ...

  5. SharePoint 2013 Nintex Workflow 工作流帮助(二)

    博客地址 http://blog.csdn.net/foxdave 工作流动作 1. Action Set(Logic and flow分组) 它是一个工作流的集合,可以理解为容器的东西.所以它本身并 ...

  6. viewController的自动扩展属性导致TableViewGroupStyle时向上填充

    self.automaticallyAdjustsScrollViewInsets = NO; 需设置这个属性

  7. poj2129 dp

    //Accepted 320 KB 47 ms //dp //dp[i][j]=1 表示用s1的前i个,s2的前j个字符能构成s3的前i+j-1个字符 //dp[i][j]=0 表示构不成 //dp[ ...

  8. javascript 构造函数中的属性与原型上属性优先级的比较

    备注: 下面这个问题是我前天看书上发现的. 按照我以前的理解, a.rename()这个方法传什么值,结果都会弹出 小a,但我看书上的demo 弹出的是大A.... 我的困惑是:  js的构造函数中的 ...

  9. 知道创宇研发技能表v2.2

    知道创宇研发技能表v2.2 2014/3/9 发布 by @知道创宇(www.knownsec.com) @余弦 & 行之 知道创宇是国内Geek十足且普遍被认为特别有前途的互联网安全公司, ...

  10. AFNetworking实现程序重新启动时的断点续传

    今天需要用AFNetworking实现断点续传的功能,但是在进行了一番研究之后,发现AFNetworking虽然支持下载文件的暂停和继续,但是程序重新启动后再次下载无法进行续传.网上有说可以通过AFD ...