LEETCODE —— Binary Tree的3 题 —— 3种非Recursive遍历
Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
- 1
- \
- 2
- /
- 3
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
- '''
- Created on Nov 18, 2014
- @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
- '''
- # Definition for a binary tree node
- # class TreeNode:
- # def __init__(self, x):
- # self.val = x
- # self.left = None
- # self.right = None
- class Solution:
- # @param root, a tree node
- # @return a list of integers
- def preorderTraversal(self, root):
- stack=[]
- vals=[]
- if(root==None): return vals
- node=root
- stack.append(node)
- while(len(stack)!=0):
- node=stack.pop()
- if(node==None): continue
- vals.append(node.val)
- stack.append(node.right)
- stack.append(node.left)
- return vals
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.
- '''
- Created on Nov 18, 2014
- @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
- '''
- # Definition for a binary tree node
- # class TreeNode:
- # def __init__(self, x):
- # self.val = x
- # self.left = None
- # self.right = None
- class Solution:
- # @param root, a tree node
- # @return a list of integers
- def inorderTraversal(self, root):
- stack=[]
- vals=[]
- visited={}
- if(root==None): return vals
- node=root
- stack.append(node)
- visited[node]=1
- while(len(stack)!=0):
- if(node.left!=None and visited.has_key(node.left)==False):
- node=node.left
- stack.append(node)
- visited[node]=1
- else:
- node=stack.pop()
- if(node==None): continue
- vals.append(node.val)
- if(node.right!=None):
- stack.append(node.right)
- node=node.right
- return vals
Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
- 1
- \
- 2
- /
- 3
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
- '''
- Created on Nov 19, 2014
- @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
- '''
- # Definition for a binary tree node
- # class TreeNode:
- # def __init__(self, x):
- # self.val = x
- # self.left = None
- # self.right = None
- class Solution:
- # @param root, a tree node
- # @return a list of integers
- def postorderTraversal(self, root):
- visited={}
- stack=[]
- vals=[]
- if(root==None): return vals
- node=root
- stack.append(node)
- visited[node]=1
- while(len(stack)!=0):
- node=stack[-1]
- if(node.left !=None and visited.has_key(node.left)==False):
- stack.append(node.left)
- visited[node.left]=1
- continue
- else:
- if(node.right!=None and visited.has_key(node.right)==False):
- stack.append(node.right)
- visited[node.right]=1
- continue
- node=stack.pop()
- if(node==None): continue
- vals.append(node.val)
- return vals
LEETCODE —— Binary Tree的3 题 —— 3种非Recursive遍历的更多相关文章
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- Binary Tree的3种非Recursive遍历
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历
题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...
- 94.Binary Tree Inorder Traversal---二叉树中序非递归遍历
题目链接 题目大意:中序遍历二叉树.先序见144,后序见145. 法一:DFS,没啥说的,就是模板DFS.代码如下(耗时1ms): public List<Integer> inorder ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- LeetCode Binary Tree Paths(简单题)
题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接DFS一次,只需要判断是否到达了叶子,是就收集答案. /** * Definition for a binary tree node. ...
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
随机推荐
- Fiddler-3 配置Fiddler监听iphone的http/https请求
电脑端可以通过Fiddler监听手机端的http请求.需要两个步骤:首先配置Fiddler,再配置手机端. 1 配置 Fiddler 允许远程设备连接: 菜单Tools - Telerik Fiddl ...
- #define用法解析
#define Add(a,b) a+b; 在一般使用的时候是没有问题的,但是如果遇到如: c * Add(a,b) * d 的时候就会出现问题,代数式的本意是a+b然后去和c,d相乘,但是因为使用了 ...
- HTML之iframe
iframe:是框架的一种形式. 属性: frameborder=0/1 表示是否显示周围边框 0--否 1--是 width,height:设置的边框宽高,具体数值不需要加单位,也可用百分比 mar ...
- PKU 1001解题代码
本来以前也写过,但是由于许多细节问题,没有AC,今天修改了一下,终于AC了,以前没有AC的具体原因总结了了一下,必须任何数的0次方都等于1没有考虑,还有就是首0和末尾0以及小数点没有处理好,下面贴代码 ...
- 关于eclipse中DDMS中Emulator Control选项卡为灰色不可用
首先先感谢版主:http://blog.csdn.net/noname666/article/details/51670905#reply 方法一的出处:http://stackoverflow.co ...
- 删除hao123这个恶心的毒瘤
最近做服务器,好好一个东西莫名其妙的被染上了这个狗皮膏药......然后我就用了各种手段删除,注册表.组策略等等都用上了,却没有丝毫办法.....最后发现的地方特别无语,居然在快捷方式的属性中加上了u ...
- SIP模块版本错误问题:the sip module implements API v??? but XXX module requires API v???
系统安装了python 2.7,继续安装PyQt4,于是依次下载sip.pyqt4源码进行安装.用以下代码测试: import PyQt4.QtGui 显示出错.错误信息:the sip module ...
- JXL操作Excel
jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文 ...
- SqlServer性能优化 即席查询(十三)
执行计划,查询类别: 1.即席查询 2.预定义查询 select c.EnglishProductCategoryName,p.EnglishProductName,p.Color,p.Siz ...
- update the UI property cross thread
this.Invoke((MethodInvoker)delegate { txtResult.Text = sbd.ToString(); // runs on UI thread });