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. java中Date与DateFormat的格式输出

    一.DateFormat java.text.DateFormat 使用 getDateInstance 来获取该国家/地区的标准日期格式.另外还提供了一些其他静态工厂方法.使用 getTimeIns ...

  2. J - MANAGER(2.4.5)

    J - MANAGER(2.4.5) Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB ...

  3. Error creating bean with name 'eurekaAutoServiceRegistration'

    spring-boot项目不断重启,报错: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creat ...

  4. 洛谷 P1583魔法照片 & P1051谁拿了最多奖学金 & P1093奖学金

    题目:https://www.luogu.org/problemnew/show/P1583 思路:sort sort sort //#include<bits/stdc++.h> #in ...

  5. 安装了nodejs后在命令行运行npm报错

    安装了nodejs后在命令行运行npm报错:Error: Cannot find module 'internal/util/types' 解决方法:删除目录“C:\Users\mengxiaobo\ ...

  6. 23. 合并K个排序链表

    一种方法是分治  类似快排的例子. 第二种使用堆,比较好理解.  堆中保存一个元素是一个链表的头部. /** * Definition for singly-linked list. * public ...

  7. [No0000105]java sdk 开发环境变量powershell 自动配置脚本

    # 设置Java SDK 环境变量 $softwares = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Unin ...

  8. ST表模板 Balanced Lineup POJ3264

    http://poj.org/problem?id=3264 题意 rmq max min之差 模板: #define _CRT_SECURE_NO_WARNINGS #include<cmat ...

  9. Django:视图views(一)

    1.环境搭建 在django中,视图负责与web请求进行交互 视图本质上是一个Python函数,定义在booktest/views.py.通过django1/urls.py路由到该视图中. 首先经过创 ...

  10. 【编译原理】c++实现自下而上语法分析器

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...