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].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
随机推荐
- Windows启动控制台登录模式
效果如下: 实现代码: Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\Lo ...
- ES6新特性6:模块Module
本文摘自ECMAScript6入门,转载请注明出处. 一.Module简介 ES6的Class只是面向对象编程的语法糖,升级了ES5的构造函数的原型链继承的写法,并没有解决模块化问题.Module功能 ...
- regex_replace
Regex_iterator方法需要输入一个正则表达式,以及一个用于替换匹配的字符串的格式化字符串:这个格式化的字符串可以通过表的转义序列引用匹配子字符串的部分内容: 转义序列 $n 替换第n个捕获的 ...
- Docker实战(八)之Web服务与应用
1.Apache 官方提供了名为httpd的Apache镜像,可以作为基础web服务镜像 Dockerfile(安装apache2) FROM httpd:2.4 COPY ./public-html ...
- CentOS6中OpenMP的运行时间或运行性能分析
OpenMp作为单机多核心共享内存并行编程的开发工具,具有编码简洁等,容易上手等特点. 关于OpenMP的入门,博主饮水思源(见参考资料)有了深入浅出,循序渐进的分析.做并行开发,做性能分析是永远逃避 ...
- 【Potplayer】视频播放器
Potplayer——视频播放器
- Xcode解决“Implicit declaration of function 'XXX' is invalid in C99” 警告或报错
1.Build Setting>>>C Language Dialect,然后选择GNU99[-std=gnu99] (选择看项目实际要求). 2.Build Setting> ...
- Redis之Redis安装(Mac OS版本)
各版本下载地址:http://download.redis.io/releases,本教程使用版本为redis-5.0.4. ##进入应用安装目录 cd /usr/local ##下载安装包 wget ...
- Hadoop系列-MapReduce基础
由于在学习过程中对MapReduce有很大的困惑,所以这篇文章主要是针对MR的运行机制进行理解记录,主要结合网上几篇博客以及视频的讲解内容进行一个知识的梳理. MapReduce on Yarn运行原 ...
- LaTeX宏包TikZ绘图示例——Go语言起源图
本例所绘图形选自<Go语言程序设计>(作者:Alan A. A. Donovan与Brian W. Kernighan)一书的前言部分. 完整代码 \documentclass{art ...