Binary Tree的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
Binary Tree的3种非Recursive遍历的更多相关文章
- LEETCODE —— Binary Tree的3 题 —— 3种非Recursive遍历
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 144 Binary Tree Preorder Traversal 二叉树的前序遍历
给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3], 1 \ 2 / 3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
随机推荐
- /dev/urandom非阻塞的发生器
JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/./urandom" /dev/urandom /dev/urandom ...
- Python之Cubes框架使用
本文主要内容包含Cubes框架的介绍和简单使用. 一. 介绍和安装 Cubes是一个轻量级的Python框架和一套工具,用于开发报告和分析应用程序,在线分析处理(OLAP),多维分析和聚合数据的浏览. ...
- 关于SpringApplication包无法导入报错问题
问题描述:一直再报红线,包始终无法导入,参考过好几个博友分享的解决方案,依然没有效果,对了补充一点SprinBoot版本为2.0.3. 问题解决:目前通过更换版本得到解决1.5.6或者1.5.8都可以 ...
- 最易懂的layui分页
该篇文章是在layui前端框架之分页基础上简洁化和详细化. 首先该示例采用的是Spring+MyBatis Plus+SpringMVC(常规的SSM框架),持久层换成MyBatis也行. 至于lay ...
- 安装framework 4.6.2的时报错 “无法建立到信任根颁发机构的证书链”
解决方案: 1.下载证书:MicrosoftRootCertificateAuthority2011.cer 2.开始→运行→MMC 3.文件→添加删除管理单元 (Ctrl+M) 4.证书→计算机账户 ...
- Objective-C(生命周期)
视图控制器生命周期 : 1)当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序 1.alloc 创建对象,分配空间 2.init(initWithNibName) 初始化对象,初始化数据 ...
- js array数组对象操作方法汇总
--------------------------更新自2018.6.11 js 数组对象操作方法如下: 1. 创建数组 var array1 = [1,2] //方法一 var array2 = ...
- Linux用户管理及用户信息查询
useradd 创建用户,更改用户信息 1.工作原理流程 使用此命令式,若不加任何参数选项,直接跟用户名,那么系统会首先读取/etc/login.defs(用户定义文件)和/etc/default/u ...
- python 第一课作用
1.使用while循环输入 1 2 3 4 5 6 8 9 10 x=0while x<10: x=x+1 if x==7: print(' ') continue print(x)#学 ...
- 【11.18总结】从SAML出发在重定向中发现的XSS漏洞
Write-up地址:How I Discovered XSS that Affects around 20 Uber Subdomains 作者:fady mohammed osman 总算回家了, ...