Binary Tree Inorder Traversal

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?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

SOL:

包括递归与非递归方法:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal1(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
rec(root, ret);
return ret;
} public void rec(TreeNode root, List<Integer> ret) {
if (root == null) {
return;
} rec(root.left, ret);
ret.add(root.val);
rec(root.right, ret);
} public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
if (root == null) {
return ret;
} Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode cur = root; while (true) {
while (cur != null) {
s.push(cur);
cur = cur.left;
} if (s.isEmpty()) {
break;
} cur = s.pop();
ret.add(cur.val); cur = cur.right;
} return ret;
}
}

LeetCode: Binary Tree Inorder Traversal 解题报告的更多相关文章

  1. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  2. LeetCode: Binary Tree Preorder Traversal 解题报告

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  3. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  4. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

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

  5. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  6. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  7. Leetcode Binary Tree Inorder Traversal

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

  8. [Leetcode] Binary tree inorder traversal二叉树中序遍历

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

  9. [LeetCode] Binary Tree Inorder Traversal 中序排序

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

随机推荐

  1. mysql join left join区别

    mysql默认的join是inner join,inner join 和 left join的区别看图:

  2. mysql用户权限管理的问题

    为了保证数据库安全,建立了若干个只能select的用户,但在权限授权的时候出现了不能连接的问题, 一个个尝试了一下,需要将 :  管理 -> SUPER项勾选才行(使用phpmyadmin),上 ...

  3. 控件View动态设置高度时会卡顿、速度慢的情况解决

    今天碰到这种情况,一直想不通是什么问题,之前一直设置高度的时候也不卡为何今天就这么卡了.做了很多小示例一直是很慢,后来试着把View的上级节点RelativeLayout的替换成了LinearLayo ...

  4. Segment Advisor

    Segment Advisor通过分析和检查AWR中关于segments的使用和增长统计信息,以及采样分析segment中的数据,找出哪些segments有可以回收的空间. Segment Advis ...

  5. ORA-01917: user or role 'PDB_DBA' does not exist

    在使用seed PDB创建新的PDB的时候,报了以下错误提示: SQL> create pluggable database pdb2 admin user admin1 identified ...

  6. MFC中无标题栏窗口的移动

    原文链接: http://blog.sina.com.cn/s/blog_6288219501015dwa.html   移动标准窗口是通过用鼠标单击窗口标题条来实现的,但对于没有标题条的窗口,就需要 ...

  7. SqlExcel使用文档及源码

    昨天帮朋友做了个小工具,以完成多表连接处理一些数据.今天下班后又做了份使用文档,不知友能看懂否?现将使用文档及源码发布如下,以供有同样需求的朋友下载. 使用文档 一.增.改.查.删 1.增(向shee ...

  8. 【转载】Eclipse中.setting目录下文件介绍

    原文:http://blog.csdn.net/huaweitman/article/details/52351394 Eclipse在新建项目的时候会自动生成一些文件.这些文件比如.project. ...

  9. java中基础数据类型的应用

    1.float 与 double float是单精度类型,占用4个字节的存储空间  double是双精度类型,占用8个字节的存储空间  1)当你不声明的时候,默认小数都用double来表示,所以如果要 ...

  10. 转:zTree高级入门:如何通过扩展节点的属性来达到是否显示节点的删除编辑等图标(按钮)

    当我们在使用ztree树组件的节点编辑功能时,只要我们引入了ztree相关节点编辑的js脚本文件: <script type="text/javascript" src=”/ ...