1. Given a binary tree, return the inorder traversal of its nodes' values.
  2.  
  3. For example:
  4. Given binary tree {1,#,2,3},
  5. 1
  6. \
  7. 2
  8. /
  9. 3
  10. return [1,3,2].
  11.  
  12. Note: Recursive solution is trivial, could you do it iteratively?

recursive 方法:

  1. /**
  2. * Definition for binary tree
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public List<Integer> inorderTraversal(TreeNode root) {
  12. ArrayList<Integer> res = new ArrayList<Integer>();
  13. if (root == null) return res;
  14. helper(root, res);
  15. return res;
  16. }
  17.  
  18. public void helper(TreeNode root, ArrayList<Integer> res) {
  19. if (root == null) {
  20. return;
  21. }
  22. helper(root.left, res);
  23. res.add(root.val);
  24. helper(root.right, res);
  25. }
  26. }

Iterative method: 参考了一下网上的思路,其实就是用一个栈来模拟递归的过程。所以算法时间复杂度也是O(n),空间复杂度是栈的大小O(logn)。

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public List<Integer> inorderTraversal(TreeNode root) {
  12. List<Integer> res = new ArrayList<>();
  13. Stack<TreeNode> stack = new Stack<>();
  14. TreeNode p = root;
  15. while (p!=null || !stack.isEmpty()) {
  16. if (p != null) {
  17. stack.push(p);
  18. p = p.left;
  19. }
  20. else {
  21. TreeNode node = stack.pop();
  22. res.add(node.val);
  23. p = node.right;
  24. }
  25. }
  26. return res;
  27. }
  28. }

Leetcode: Binary Tree Inorder Transversal的更多相关文章

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

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

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

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

  3. Leetcode Binary Tree Inorder Traversal

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

  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] Binary Tree Inorder Traversal 中序排序

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

  6. leetcode Binary Tree Inorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  7. Leetcode: Binary Tree Postorder Transversal

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

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

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

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

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

随机推荐

  1. Oracle 12C 创建用户连接pdb

    测试环境: C:\ora12c\product\12.1.0\dbhome_1\BIN>sqlplus.exe /nolog SQL*Plus: Release 12.1.0.1.0 Produ ...

  2. mysql索引覆盖之innodb和myisam效率问题

    问题: create table A (    id varchar(64) primary key,    ver int,    ... ) 我的表有几个很长的字段varchar(3000) 在i ...

  3. 用Android Studio导出jar给Unity3D用

    1.新建一个Android Studio工程,选择空Activity 2.创建一个Module 3.将Unity的依赖jar包拷贝到工程的libs下 4.增加Java代码 内容修改如下 package ...

  4. [Windows] 使用SC 命令管理与配置服务

    sc config wuauserv start= demand sc config wuauserv start= disabled

  5. android 关于view的onTouch和onClick同时触发解决方案

    extends:http://blog.sina.com.cn/s/blog_aa0bd5950101gbwt.html 做了一个悬浮窗,需要处理onTouch和onClick事件, 1 定义一个bo ...

  6. Ubuntu 16.04系统下apt-get和dpkg区别

    两者的区别是dpkg绕过apt包管理数据库对软件包进行操作,所以你用dpkg安装过的软件包用apt可以再安装一遍,系统不知道之前安装过了,将会覆盖之前dpkg的安装.1.dpkg是用来安装.deb文件 ...

  7. Ubuntu16.04双网卡主备配置

    前几日写了一篇Ubuntu14.04双网卡主备配置,没成想变化总是这么快,今日安装某软件,提示最匹配的ubuntu版本是16.04,作为一个码农能有什么办法,只能不断去适应变化.拥抱变化. 首先16. ...

  8. php下载文件,解压文件,读取并写入新文件

    以下代码都是本人在工作中遇到的问题,并完成的具体代码和注释,不多说,直接上代码: <?php      //组织链接      $dataurl = "http://118.194.2 ...

  9. 基础回顾—list遍历4种

    ()for each for(bject o :list) { } [java] view plain copy ()Iterator Iterator iter = list.iterator(); ...

  10. ubuntu16.04配置tensorflow-gpu环境

    1.安装驱动 参考: 史上最全的ubuntu16.04安装nvidia驱动+cuda9.0+cuDnn7.0 https://blog.csdn.net/qq_31215157/article/det ...