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

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

  1. 1
  2. \
  3. 2
  4. /
  5. 3

return [1,3,2].

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

题目大意:中序遍历一个二叉树,递归的方案太low,用迭代的方式来写?

解题思路:不用递归,那就自己实现栈呗

1、首先节点入栈,处理当前节点左孩子,并且压入栈,当左节点非空,循环遍历;

2、找到第一个左孩子为空的节点,将此节点出栈,将节点值加入结果链表,并把当前节点设为右孩子;

3、循环到栈为空。

  1. public List<Integer> inorderTraversal(TreeNode root) {
  2. List<Integer> res = new ArrayList<>();
  3. Stack<TreeNode> stack = new Stack<>();
  4. TreeNode curr = root;
  5. while (curr != null || !stack.isEmpty()) {
  6. while (curr != null) {
  7. stack.add(curr);
  8. curr = curr.left;
  9. }
  10. curr = stack.pop();
  11. res.add(curr.val);
  12. curr = curr.right;
  13. }
  14. return res;
  15. }

Binary Tree Inorder Traversal ——LeetCode的更多相关文章

  1. Binary Tree Inorder Traversal -- LeetCode 94

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

  2. Binary Tree Inorder Traversal leetcode java

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

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

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

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

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

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

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  6. 49. leetcode 94. Binary Tree Inorder Traversal

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

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

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

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

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

  9. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

随机推荐

  1. 处理ASP.NET 请求(IIS)(2)

    ASP.NET与IIS是紧密联系的,由于IIS6.0与IIS7.0的工作方式的不同,导致ASP.NET的工作原理也发生了相应的变化. IIS6(IIS7的经典模式)与IIS7的集成模式的不同 IIS6 ...

  2. C# 内存管理优化畅想(二)---- 巧用堆栈

    这个优化方法比较易懂,就是对于仅在方法内部用到的对象,不再分配在堆上,而是直接在栈上分配,方法结束后立即回收,这将大大减轻GC的压力. 其实,这个优化方法就是java里的逃逸分析,不知为何.net里没 ...

  3. Singleton设计模式的一种见解

    单实例Singleton设计模式可能是被讨论和使用的最广泛的一个设计模式了,这可能也是面试中问得最多的一个设计模式了.这个设计模式主要目的是想在整个系统中只能出现一个类的实例.这样做当然是有必然的,比 ...

  4. 一种实现C++反射功能的想法(三)

    如何实现类型名跟类型的对应, 我们很容易想到map, 没错, 就是使用map实现的. std::map<std::string, .....>, 等下, 第二部分该填什么类型, 一个函数指 ...

  5. Perl数组: shift, unshift, push, pop

    pop pop函数会删除并返回数组的最后一个元素. .. ; $fred = pop(@array); # $fred变成9,@array 现在是(5,6,7,8) $barney = pop @ar ...

  6. linxu 挂载分区

    1. 添加新硬盘 设置 -> Storage -> SATA控制器->右击,选择“添加虚拟硬盘” 然后,根据需求创建合适的硬盘 2. 重启虚拟机 查看现有系统的磁盘空间 sudo f ...

  7. 织梦dedecms后台发布文章不自动更新首页与栏目列表页

    dedecms发文章不自动更新首页也列表页解决办法如下: 登陆dedecms后台,找到“系统”“系统基本参数”“性能选项”,把“arclist标签调用缓存”设置成0,然后把“发布文章后马上更新网站主页 ...

  8. @import————————css代码内部链接另外css

    在css代码里这样可以链接另外的css @import url("style.css");   @import语法结构 @import + 空格+ url(CSS文件路径地址); ...

  9. 使用PHP预定义变量得到url地址及相关参数

    获取url地址栏参数多种方法:$_SERVER["SERVER_PORT"]//获取端口$_SERVER['HTTP_HOST']//获取域名或主机地址 如www.sina.com ...

  10. apache 设置404页面

    这几天用xampp搭建了一套环境,后来发现在网页访问出现404的时候xampp显示的内容不安全,把apache.php还有一些其它的版本都会显示 出来,所以想自己设置一个404的页面,在网上找了一些资 ...