Leetcode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
本题如果用recursive的方法非常简单。这里主要考察用iterative的方法求解。例如: [1,3,4,6,7,8,10,13,14]

从8开始依次将8,3,1 push入栈。这时root=None,每当root=None时就从stack里pop一个node出来。并将root定义成3的右子树。
这里其实有一个中间步骤,就是pop出1来之后,root被定义成了1的right kid,然而1并没有right kid。但是这是stack里面还有[8, 3]。所以下一次运行L9的while时,我们会跳过L10-L12直接从stack里面在pop出3来。
然后又是用while root将最以3为root的tree左边的一条臂全都推入栈中。
所以这个解法的巧妙之处就在于我们其实每次从stack里面pop出一个node时,都试图将root挪到他的右子树的root。
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
stack = []
while stack or root:
while root:
stack.append(root)
root = root.left
root = stack.pop()
ans.append(root.val)
root = root.right
return ans
Leetcode 94. Binary Tree Inorder Traversal的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆
二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...
随机推荐
- databtables 设置(显示)行号
var table = $('#priceStrategtyTable').DataTable({ "rowCallback": function( row, da ...
- python-切片 迭代 生成器
1 切片操作 >>> L ['aaa', 'bbb', 'ccc', 'ddd'] >>> L[0:3] ['aaa', 'bbb', 'ccc'] >> ...
- curl操作CouchDB
couchdb 服务器地址: 127.0.0.1 端口:5984 添加数据库 连接到couchdb curl -X GET http://127.0.0.1:5984 {"couchdb&q ...
- MySQL修改root账号密码
MySQL数据库中如何修改root用户的密码呢?下面总结了修改root用户密码的一些方法 1: 使用set password语句修改 mysql> select user(); +----- ...
- ORACLE 移动数据文件 控制文件 重做日志文件
ORACLE数据库有时候需要对存储进行调整,增加分区.IO调优等等,此时需要移动数据文件.重做日志文件.控制文件等等,下文结合例子总结一下这方面的知识点. 进行数据文件.重做日志文件.控制文件的迁移前 ...
- SSRS Reports 2008性能优化案例
我们的一个Reporting Service服务上部署了比较多的SSRS报表,其中有一个系统的SSRS报表部署后,执行时间相对较长,加之供应商又在ASP.NET页面里面嵌套了Reporting Ser ...
- JVM之Class文件结构
每一个class文件对应一个类或者接口,但是一个类或者接口不一定生成class文件,classloader直接生成. 8为字节为基础的二进制流,各个数据项按照严格的顺序排列在class文件中,没有任何 ...
- Mysql时间类型处理
关于Mysql中时间的处理 最近在读<人类简史>,读第二遍.只有晚上睡觉之前读一点点,有时候觉得一天可以抽出一个专门的时间来看书了,效率应该能高不少. 另外分享个网址可以随心创作 这里有一 ...
- GL.IssuePluginEvent 发布插件事件
Description 描述 Send a user-defined event to a native code plugin. 发送一个用户定义的事件到一个本地代码插件. Rendering in ...
- css实现在图片上显示文字
一. 准备工作 1. 点击此下载 相关的文件 二. 浏览器中运行 play-img.html 文件,即可显示效果 三. 效果图