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 ...
随机推荐
- LCD底层驱动分析
根据分析的框架,自己写一个LCD驱动程序 1分析LCD硬件原理图 Von和Voff接的是一个电源电路,通过LCD_POWER接的是GPG4来控制LCD电源,高电平表示开启LCD电源 VM接的是CPU的 ...
- iOS 指定圆角个数
需要实现的效果很明确,只要左上和右上两个地方圆角,以前都是通过layer 直接设置四个角都变成圆角,然后我就开始了强大的搜索功能 然后我就获得了我想要的东西 技术链接:http://www.xuebu ...
- Percona Server 5.6.33-79.0 发布
Percona Server 5.6.33-79.0 发布了,该版本基于 MySQL 5.6.33,包含了所有的 bug 修复,是Percona Server 5.6 系列中的正式版本.该版本主要是修 ...
- 如何查看前端部署的tracker代码
1.www.gov.cn 2.F12>Source>左侧选择static.gridsumdissector.com/js 3.点击代码下方区域的中括号,展开代码preety print{}
- 为什么要学习java?
前面说了什么是java java只是一门语言,中文,英语,c++,c#等等数之不尽的语言 java的应用领域: 1)安卓应用 2)金融业服务器的应用 3)Java Web应用 4)软件工具 5)交易应 ...
- Ubuntu 14.10 创建虚拟网卡实现桥接网络
目标需求:在Ubuntu主机创建一张虚拟网卡,使得搭建在VirtualBox中的其他主机能借助虚拟网卡构建的局域网进行相互间的通讯 步骤一:准备工具包 安装 uml-utilities 以及 brid ...
- 安卓性能测试工具-GT,安测试
GT: 是腾讯出品的一款APP的随身调测平台,它是直接运行在手机上的“集成调测环境”(IDTE, Integrated Debug&Test Environment).利用GT,仅凭一部 ...
- 基于HTML5的网络拓扑图
电信网管系统中,设备状态信息的实时展示非常重要,通常会挂载一堆图标来展示状态或告警信息,图标的信息量有限,有时需要更详细的面板,甚至以图表的形式展现,本文将结合最近客户提到的需求,使用 Qunee1. ...
- css width height
css 中body 的默认宽度是100%,但是默认高度为0px
- NOIP 考前 数论复习
POJ 2891 x=r1 (mod a1) x=r2 (mod a2) x=a1*x+r1,x=a2*y+r2; a1*x-a2*y=r2-r1; 用Extend_Gcd求出m1*x+m2*y=d; ...