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].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
随机推荐
- ethers.js-3-Providers
Providers A Provider abstracts a connection to the Ethereum blockchain, for issuing queries and send ...
- h5py
解决办法: sudo apt-get install libhdf5-dev sudo apt-get install python-h5py
- iteritems()与items()
iteritems:以迭代器对象返回字典键值对 item:以列表形式返回字典键值对 >>> dic = {'a':3,'c':1,'b':2} >>> print ...
- KVM的初始化过程
之前打算整理一下在Guest VM, KVM, QEMU中IO处理的整个流程,通过查阅资料和阅读源码,已经大致知道IO在Guest KVM中的处理流程.当想要整理IO在KVM和QEMU中的处理时,发现 ...
- EasyX_无法填充圆颜色的问题
官网:https://www.easyx.cn/ 在线帮助文档:https://docs.easyx.cn/ 目标:生成一个边框为黄色,填充为蓝色的圆 遇到的问题:使用以下代码,只能生成边框为黄色的圆 ...
- ThinkPhp5学习之新手博客
前端框架来源网络,后端框架采用 ThinkPhp 5 开发 参考资料:哔哩哔哩 ThinkPHP5.1新手博客项目实战 项目地址:https://github.com/yjy1/tp5
- MAC上安装GCC失败
问题 在用brew安装GCC时, 报了如下错误. ➜ ~ brew install gcc ==> Installing dependencies for gcc: isl, mpfr and ...
- 一个简单python爬虫的实现——爬取电影信息
最近在学习网络爬虫,完成了一个比较简单的python网络爬虫.首先为什么要用爬虫爬取信息呢,当然是因为要比人去收集更高效. 网络爬虫,可以理解为自动帮你在网络上收集数据的机器人. 网络爬虫简单可以大致 ...
- 【10.14】Bug Bounty Write-up总结
我很喜欢今天的看到的write-up,因为作者是针对他对一个网站整体进行漏洞挖掘的过程写的,内容包括几个不同的漏洞,从中能够学习到怎样系统性的挖掘漏洞. write-up地址:[Bug bounty ...
- WPF MessageContract DataContract
个人理解: DataContract:都序列化在消息体内. MessageContract :能够定义数据字段的序列化位置,比如在头部或者在消息体内.