本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876657

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

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,3,2].

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

思路:

(1)题意为中序遍历二叉树。遍历顺序为左—>根—>右。

(2)考虑到用递归比较简单,本文使用递归的思想进行解决,由于比较简单这里不累赘,详见下方代码。

(3)希望本文对你有所帮助。

算法代码实现如下:

/**
 * @author liqq
 */
public List<Integer> inorderTraversal(TreeNode root) {

	List<Integer> result = new LinkedList<Integer>();

	if (root != null) {
		in_order(result, root.left);
		result.add(root.val);
		in_order(result, root.right);
	}
	return result;
}

private void in_order(List<Integer> result, TreeNode curr) {
	if (curr != null) {
		in_order(result, curr.left);
		result.add(curr.val);
		in_order(result, curr.right);
	}
}

Leetcode_94_Binary Tree Inorder Traversal的更多相关文章

  1. LintCode Binary Tree Inorder Traversal

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

  2. 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月3日(4) Binary Tree Inorder Traversal

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

  4. 49. leetcode 94. Binary Tree Inorder Traversal

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

  5. LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium

    题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...

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

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

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

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

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

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

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

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

随机推荐

  1. python3中替换python2中cmp函数的新函数分析(lt、le、eq、ne、ge、gt)

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/11332589 作者:sushengmiyan 在python2中我们经常会使用cmp函 ...

  2. proc文件系统探索 之 以数字命名的目录

    在proc根目录下,以数字命名的目录表示当前一个运行的进程,目录名即为进程的pid.其内的目录和文件给出了一些关于该进程的信息. niutao@niutao-desktop:/proc/6584$ l ...

  3. linux:CPU私有变量(per-CPU变量)

    一.简介2.6内核上一个新的特性就是per-CPU变量.顾名思义,就是每个处理器上有此变量的一个副本.per-CPU的最大优点就是,对它的访问几乎不需要锁,因为每个CPU都在自己的副本上工作.task ...

  4. Studio 一些使用

    1,配置: W:\android_tools\AndroidStudio2.1.3_SDK\android-studio-ide-141.2456560-windows\android-studio\ ...

  5. Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入

    工具类 package --; import java.math.BigDecimal; /** * Created by kongqw on 2015/12/10. */ public final ...

  6. Spark Scheduler模块源码分析之DAGScheduler

    本文主要结合Spark-1.6.0的源码,对Spark中任务调度模块的执行过程进行分析.Spark Application在遇到Action操作时才会真正的提交任务并进行计算.这时Spark会根据Ac ...

  7. [ExtJS5学习笔记]第二十八节 sencha ext js 5.1.0发布版本正式发布 extjs doc下载地址

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/41911539 本文作者:sushengmiyan ------------------ ...

  8. Unity UGUI图文混排源码(三) -- 动态表情

    这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...

  9. Android开发学习之路--Annotation注解简化view控件之初体验

    一般我们在写android Activity的时候总是会在onCreate方法中加上setContentView方法来加载layout,通过findViewById来实现控件的绑定,每次写这么多代码总 ...

  10. Description Resource Path Location Type AndroidManifest.xml file missing!

    这个问题又找了好久.国内回答的确不敢恭维. 本回答来自谷歌:   This is build issue. Go to Menu in eclipse, Project>clean then P ...