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. [LintCode] Max Points on a Line 共线点个数

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  2. 依赖注入容器之Castle Windsor

    一.Windsor的使用 Windsor的作为依赖注入的容器的一种,使用起来比较方便,我们直接在Nuget中添加Castle Windsor,将会自动引入Castle.Core 和 Castle.Wi ...

  3. vue axios跨域请求,代理设置

    在config下的index.js中的dev下的 proxyTable{}中设置代理 proxyTable: { '/api': { target: 'http://10.0.100.7:8081', ...

  4. mysql的多表查询join

    http://blog.csdn.net/jintao_ma/article/details/51260458 https://zhidao.baidu.com/question/1304158100 ...

  5. Ubuntu 安装 .bundle 文件

    ubuntu安装VMware-Workstation-Full-15.0.2-10952284.x86_64.bundle 一.*.bundle 文件比较特殊,只有在给它了执行权限后才能执行安装操作. ...

  6. 网络层block,delegate之优劣分析

    正常情况下, block 缺点: 1.block很难追踪,难以维护 2.block会延长先关对象的生命周期 block会给内部所有的对象引用计数+1, 一方面会带来潜在的循环引用(retain cyc ...

  7. centos7安装Apache

    1.下载安装包wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.37.tar.gz 2.解压tar zxvf httpd-2.4.37.ta ...

  8. [daily][qemu][libvirt] 使用libvirt管理qemu

    别人创建的虚拟机.用libvirt做的配置. 我一直是手写qemu脚本的,不会用virtsh,所以,学一下. ------------------ 先来个arch的文档: https://wiki.a ...

  9. BTree和B+Tree详解

    https://www.cnblogs.com/vianzhang/p/7922426.html B+树索引是B+树在数据库中的一种实现,是最常见也是数据库中使用最为频繁的一种索引.B+树中的B代表平 ...

  10. 启动虚拟机提示"Units specified don’t exist SHSUCDX can’t install"

    新建虚拟机快速分区后启动报"Units specified don’t exist SHSUCDX can’t install",试过网上说的 修改BIOS设置方法不起作用 修改虚 ...