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?

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

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

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

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

3、循环到栈为空。

     public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()) {
while (curr != null) {
stack.add(curr);
curr = curr.left;
}
curr = stack.pop();
res.add(curr.val);
curr = curr.right;
}
return res;
}

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 Boilerplate 工作单元

    从上往下说起,框架使用castle拦截器,拦截实现了IApplication.IRepository接口的所有方法,和使用了UnitOfWork 特性的方法,代码如下 internal class U ...

  2. mysql隐藏文件一定要删除彻底

    之前部署自己的服务器机器的时候 机器的mysql密码是不知道的.彻底删除了软件之后还是解决不了问题,而且我把MYSQL在C盘的隐藏文件也给删除了.但是还是不行 最后我偶然发现一个方法去找隐藏问题.我之 ...

  3. spring验证事务的代码,用到了mockito

    package *.withdraw; import javax.annotation.Resource; import org.junit.Before; import org.junit.Test ...

  4. [转]Web UI 设计命名规范

    来源:http://blog.bingo929.com/web-ui-design-name-convention.html 一.网站设计及基本框架结构: 1.    Container “conta ...

  5. Word 中标题的编号变成黑框

    问题: 在使用Word编写文档时,提前拟好的标题编号会突然变成黑框(黑色的方框,黑色的矩形),如下图 解决方案: 1.将光标定位到标题中,紧邻黑框的右侧 2.按键盘左方向键使方框变成黑色 4.按键盘的 ...

  6. Scrum教练不应兼任product owner

    ScrumMasters Should Not Also Be Product Owners(中文翻译) December 2, 2014 by Mike Cohn 翻译:2015.2.18 by o ...

  7. jQuery 获取并设置 CSS 类

    通过 jQuery,可以很容易地对 CSS 元素进行操作. 切换 CSS 类 jQuery 操作 CSS jQuery 拥有若干进行 CSS 操作的方法.我们将学习下面这些: addClass() - ...

  8. ubuntu12.04安装QQ2013

    1.下载Longene QQ2013SP6 http://pan.baidu.com/s/1hq83fWo 2.安装 1)如果之前安装过旧版本需要先卸载(通过dpkg -l | grep qq查看). ...

  9. 第五篇、 WebSphere8.5的安装

    一.前言 WebSphere Application  Server 是IBM企业级应用服务器,与WAS6,WAS7相比较而言 WAS8发生了很大的改变,其安装介质和以前截然不同,该篇章中对于不同的安 ...

  10. ios开发之ios9UIWebView不显示网页问题

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...