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. Linux系统中的文件权限

    r(read)         4    可读 w(write)   2     可写 x()           1     可执行 644            755 rw-r-r        ...

  2. spark - Locality Level

    这几个值在图中代表 task 的计算节点和 task 的输入数据的节点位置关系 PROCESS_LOCAL: 数据在同一个 JVM 中,即同一个 executor 上.这是最佳数据 locality. ...

  3. 现在越来越喜欢用ajax传值了,这样能让网站的体验性很好,今天就总结了一下常用的

    这是不用循环的方法 就是传过来的是一位数组 //编辑党建分类 function gk_bj(id){ $.post("{:U('Luser/lei_edlt')}",{id:id} ...

  4. python金融与量化分析----Jupyter Notebook使用

    Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言.在本文中,我们将介绍 Jupyter notebook 的主要特性,以 ...

  5. STL的基本介绍

    STL是标准模板库,现在是c++的一部分 STL被组织为下面的17个头文件:<algorithm>.<deque>.<functional>.<iterato ...

  6. F#周报2018年第51期

    新闻 有经验的开发者选择F# 2018年10佳技术讲话 试用F#开发WebAssembly Fable.Remoting: 刷新访问令牌 F#开发WebAssembly现在可以使用代码补全 Rider ...

  7. ROS中.launch文件的remap标签详解

    https://www.cnblogs.com/LiuQiang921202/p/7679943.html

  8. mysql 语句 字段 和结构主键外键的增删改

    primary key 主键  notnull 不为空 unique 唯一       foreign key(外键) references t1(id)        auto_increment ...

  9. python中的str和repr函数的区别

    看了一些网上的解释,最主流的解释是“str是给人看的,repr是给机器看的”,如果已经理解了的,这句话是对的,但是是有问题的,对于没懂的,这句话是无法理解的. 我来尝试解释一下.先直译一下官方文档: ...

  10. 【RMAN】RMAN-05001: auxiliary filename conflicts with the target database

    oracle 11.2.0.4 运行以下脚本,使用活动数据库复制技术创建dataguard备库报错rman-005001: run{ duplicate target database for sta ...