Pre: node 先,                      Inorder:   node in,           Postorder:   node 最后

PreOrder Inorder PostOrder
node-> left -> right left -> node ->right left -> right ->node

Recursive method

实际上代码是一样, 就是把ans.append(root.val) 放在如上表先, 中, 后就是pre, in, post order了.

1) PreOrder traversal

ans = []
def preOrder(self, root):
if not root: return
ans.append(root.val)

preOrder(root.left)
preOrder(root.right) preOrder(root)
return ans

2) Inorder traversal   Worst S: O(n), average is O(lgn)

ans = []
def inOrder(self, root):
if not root: return
inOrder(root.left)
ans.append(root.val)
inOrder(root.right) inOrder(root)
return ans

3) PostOrder traversal

ans = []
def postOrder(self, root):
if not root: return
postOrder(root.left)
postOrder(root.right)
ans.append(root.val)

postOrder(root)
return ans

Iterable method

1) Preorder traversal --- Just use stack.

node-> left -> right

def Preorder(self, root):
if not root: return []
ans, stack = [], [root]
while stack:
node = stack.pop()
ans.append(node.val)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
return ans

2) inOrder traversal

left -> node ->right

def inOrder(self, root):
ans, stack = [], []
while True:
while root:
stack.append(root)
root = root.left
if not stack: return ans
node = stack.pop()
ans.append(node.val)
root = node.right

seconde inOrder traversal

def inOrder(self, root):
ans, stack = [], []
while stack or root:
if root:
stack.append(root)
root = root.left
else:
node = stack.pop()
ans.append(node.val)
root = node.right
return ans

3) PostOrder traversal

left -> right ->node

由于我们已经知道如何用preorder, 所以我们知道用 node-> left -> right, 所以我们可以用类似于preorder的做法, 将node-> right -> left 做出来, 最后返回reverse 的ans即可.

def PostOrder(self, root):
if not root: return []
stack, ans = [root], []
while stack:
node = stack.pop()
ans.append(node.val)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
return ans[::-1]

second PostOrder traversal

利用hash table, 但是我们直接将这个hash table append进入stack, 跟node形成一个tuple.

def PostOrder(self, root):
if not root: return []
stack, ans = [(root, False)], []
while stack:
node, visited = stack.pop()
if visited:
ans.append(node.val)
else:
stack.append((node, True))
if node.right:
stack.append((node.right, False))
if node.left:
stack.append((node.left, False))
return ans

Questions:

[LeetCode]94, 144, 145 Binary Tree InOrder, PreOrder, PostOrder Traversal_Medium

[LeetCode] 589. N-ary Tree Preorder Traversal_Easy

[LeetCode] 590. N-ary Tree Postorder Traversal_Easy

[LeetCode] 98. Validate Binary Search Tree_Medium

[LeetCode] 230. Kth Smallest Element in a BST_Medium tag: Inorder Traversal

[LeetCode] 285. Inorder Successor in BST_Medium tag: Inorder Traversal

[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal

[LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree_Medium tag: Preorder Traversal, tree

[LeetCode] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack

[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal的更多相关文章

  1. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  2. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  3. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  4. (N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  5. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  6. Java for LeetCode 145 Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. leetcode 145. Binary Tree Postorder Traversal ----- java

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  8. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  9. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

随机推荐

  1. day_6.10py面试题:访问百度的过程

    DNS : 解析域名 DHCP:一种协议,自动分配ip 发现局域网中没有ip的电脑分配ip 面试题: 访问百度的整个过程 打开浏览器,访问百度的过程: 1.我的电脑确定有无网关,arp得到默认网管ma ...

  2. 自定义 vim

    官网 插件列表 Vundle 插件管理器 windows cmder 安装 Vundle git clone https://github.com/VundleVim/Vundle.vim.git ~ ...

  3. linux命令瞎记录find xargs

    1.创建多个文件 touch test{0..100}.txt 2.重定向 “>>” 追加重定向,追加内容,到文件的尾部 “>” 重定向,清除原文件里面所有内容,然后把内容追加到文件 ...

  4. php之函数

    scope(空间) unpack (解压) Traversable (穿越) performance(性能) experiment (检验) properties (属性) trailing (尾随) ...

  5. wpf中通过ObjectDataProvider实现文本框的双向数据绑定(ps:适用于在文本框比较多的时候使用)

    前端代码: 也页面的xaml中引入ObjectDataProvider: <Window.Resources> <ResourceDictionary> <ObjectD ...

  6. 深探树形dp

    看到同学在写一道树形dp,好奇直接拿来写,发现很不简单. 如图,看上去是不是很像选课,没错这不是选课,升级版吧,多加了点东西罢了.简单却调了一晚上和一上午. 思路:很简单强联通分量+缩点+树形dp.直 ...

  7. day2_python基础

    1.变量: 用来存东西的,左边是名字,右边是值 2.python中的单引号.双信号.三引号 单引号和双引号和三引号没什么区别,用哪个都可以,如果定义字符串里面如果有单引号,则外面用双引号;如果字符串里 ...

  8. tornado框架&三层架构&MVC&MTV&模板语言&cookie&session

    web框架的本质其实就是socket服务端再加上业务逻辑处理, 比如像是Tornado这样的框架. 有一些框架则只包含业务逻辑处理, 例如Django, bottle, flask这些框架, 它们的使 ...

  9. LeetCode 953 Verifying an Alien Dictionary 解题报告

    题目要求 In an alien language, surprisingly they also use english lowercase letters, but possibly in a d ...

  10. 《mongoDB》基本操作-创建/更新/删除文档

    一:基本操作 - db; 当前选择的集合(等于数据库名) > db demo -  use db_name; 选择你要操作的集合 > use demo switched to db dem ...