bobby.peng

zhendecai

  • 主页
  • 技术
  • 杂谈
  • Tips
所有文章 友链 关于我

bobby.peng

zhendecai

  • 主页
  • 技术
  • 杂谈
  • Tips

Tree Traversal

2017-03-27

pre order

pre order

Pre-order: F, B, A, D, C, E, G, I, H.

  1. Check if the current node is empty / null
  2. Display the data part of the root (or current node).
  3. Traverse the left subtree by recursively calling the pre-order function.
  4. Traverse the right subtree by recursively calling the pre-order function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> res = new LinkedList<Integer>();
if(root!=null) s.push(root);
while(!s.isEmpty()){
TreeNode curr = s.pop();
res.add(curr.val);
if(curr.right!=null) s.push(curr.right);
if(curr.left!=null) s.push(curr.left);
}
return res;
}
}

in order

in order

In-order: A, B, C, D, E, F, G, H, I.

  1. Check if the current node is empty / null
  2. Traverse the left subtree by recursively calling the in-order function.
  3. Display the data part of the root (or current node).
  4. Traverse the right subtree by recursively calling the in-order function.

    In a search tree, in-order traversal retrieves data in sorted order.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> res = new LinkedList<Integer>();
    Stack<TreeNode> s = new Stack<TreeNode>();
    //先将最左边的节点都push进栈
    if(root!=null){
    pushAllTheLeft(s, root);
    }
    while(!s.isEmpty()){
    TreeNode curr = s.pop();
    res.add(curr.val);
    //如果有右子树,将右节点和右子树的最左边的节点都push进栈
    if(curr.right != null){
    pushAllTheLeft(s, curr.right);
    }
    }
    return res;
    }
    private void pushAllTheLeft(Stack<TreeNode> s, TreeNode root){
    s.push(root);
    while(root.left!=null){
    root = root.left;
    s.push(root);
    }
    }
    }

post order

post order

Post-order: A, C, E, D, B, H, I, G, F.

  1. Check if the current node is empty / null
  2. Traverse the left subtree by recursively calling the post-order function.
  3. Traverse the right subtree by recursively calling the post-order function.
  4. Display the data part of the root (or current node).

    The trace of a traversal is called a sequentialisation of the tree. The traversal trace is a list of each visited root. No one sequentialisation according to pre-, in- or post-order describes the underlying tree uniquely. Given a tree with distinct elements, either pre-order or post-order paired with in-order is sufficient to describe the tree uniquely. However, pre-order with post-order leaves some ambiguity in the tree structure.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
    Stack<PowerNode> s = new Stack<PowerNode>();
    List<Integer> res = new LinkedList<Integer>();
    if(root!=null) s.push(new PowerNode(root, false));
    while(!s.isEmpty()){
    PowerNode curr = s.peek();
    //如果是第二次访问,就计算并pop该节点
    if(curr.visited){
    res.add(curr.node.val);
    s.pop();
    } else {
    //如果是第一次访问,就将它的左右节点加入stack,并设置其已经访问了一次
    if(curr.node.right!=null) s.push(new PowerNode(curr.node.right, false));
    if(curr.node.left!=null) s.push(new PowerNode(curr.node.left, false));
    curr.visited = true;
    }
    }
    return res;
    }
    private class PowerNode {
    TreeNode node;
    boolean visited;
    public PowerNode(TreeNode n, boolean v){
    this.node = n;
    this.visited = v;
    }
    }
    }

参见wiki

参见[leetcode] Binary Tree Traversal 二叉树遍历

  • java
  • data-structure
  • tech

扫一扫,分享到微信

微信分享二维码
wow的岁月
垃圾回收
Like Issue Page
Loading comments...
Login with GitHub
Styling with Markdown is supported
Powered by Gitment
© 2018 bobby.peng
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • tree
  • cocurrent
  • java
  • 复习
  • mysql
  • data-structure
  • ximalaya
  • redis
  • wow
  • 转载
  • jvm
  • concurrent
  • aqs
  • Spring
  • io

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 麻子
  • 王鸿缘
  • 美团技术博客
  • 何登成的技术博客
  • 并发编程网
Blizzard fans
wow & overwatch
Arena master & Gladiator