zoukankan      html  css  js  c++  java
  • Binary Tree Traversal

    1. Preorder Tree Traversal

     1 // Solution 1. With Stack
     2 // Time Complexity : O(n)
     3 // Space Complexity: O(h) ~ O(log n)
     4 public class Solution {
     5     public List<Integer> preorderTraversal(TreeNode root) {
     6         ArrayList<Integer> list = new ArrayList<Integer>();
     7         if (root == null) return list;
     8         
     9         Stack<TreeNode> stack = new Stack<TreeNode>();
    10         stack.push(root);
    11         TreeNode cur = null;
    12         while (!stack.isEmpty()) {
    13             cur = stack.pop();
    14             list.add(cur.val); // Do manipulation here
    15             if (cur.right != null) stack.push(cur.right);
    16             if (cur.left != null) stack.push(cur.left);
    17         }
    18         
    19         return list;
    20     }
    21 }
     1 // Solution 2. Morris Traversal
     2 // Time Complexity : O(n)
     3 // Space Complexity : O(1)
     4 public class Solution {
     5     public List<Integer> preorderTraversal(TreeNode root) {
     6         List<Integer> list = new ArrayList<Integer>();
     7         
     8         TreeNode cur = root;
     9         TreeNode pre = null;
    10         while (cur != null) {
    11             if (cur.left != null) {
    12                 pre = cur.left;
    13                 while (pre.right != null && pre.right != cur) { // link / unlink
    14                     pre = pre.right;
    15                 }
    16                 if (pre.right == null) {
    17                     list.add(cur.val);
    18                     pre.right = cur;
    19                     cur = cur.left;
    20                 } else {
    21                     pre.right = null;
    22                     cur = cur.right;
    23                 }
    24             } else {
    25                 list.add(cur.val);
    26                 cur = cur.right;
    27             }
    28         }
    29         
    30         return list;
    31     }
    32 }

    2. Inorder Tree Traversal

     1 // Solution 1. With Stack
     2 // Time Complexity : O(n)
     3 // Space Complexity: O(h) ~ O(log n)
     4 public class Solution {
     5     public List<Integer> inorderTraversal(TreeNode root) {
     6         ArrayList<Integer> list = new ArrayList<Integer>();
     7         if (root == null) return list;
     8         
     9         TreeNode cur = root;
    10         Stack<TreeNode> stack = new Stack<TreeNode>();
    11         while (cur != null || !stack.isEmpty()) {
    12             if (cur != null) {
    13                 stack.push(cur);
    14                 cur = cur.left;
    15             } else {
    16                 cur = stack.pop();
    17                 list.add(cur.val); // Do manipulation here
    18                 cur = cur.right;
    19             }
    20         }
    21         
    22         return list;
    23     }
    24 }
     1 // Solution 2. Morris Traversal
     2 // Time Complexity : O(n)
     3 // Space Complexity : O(1)
     4 public class Solution {
     5     public List<Integer> inorderTraversal(TreeNode root) {
     6         List<Integer> list = new ArrayList<Integer>();
     7         
     8         TreeNode cur = root;
     9         TreeNode pre = null;
    10         while (cur != null) {
    11             if (cur.left != null) {
    12                 pre = cur.left;
    13                 while (pre.right != null && pre.right != cur) {
    14                     pre = pre.right;
    15                 }
    16                 if (pre.right == null) {
    17                     pre.right = cur;
    18                     cur = cur.left;
    19                 } else {
    20                     pre.right = null;
    21                     list.add(cur.val);
    22                     cur = cur.right;
    23                 }
    24             } else {
    25                 list.add(cur.val);
    26                 cur = cur.right;
    27             }
    28         }
    29         return list;
    30     }
    31 }

    3. Postorder Tree Traversal

     1 // Solution 1. With Stack
     2 // Time Complexity : O(n)
     3 // Space Complexity: O(h) ~ O(log n)
     4 public class Solution {
     5     public List<Integer> postorderTraversal(TreeNode root) {
     6         ArrayList<Integer> list = new ArrayList<Integer>();
     7         if (root == null) return list;
     8         
     9         TreeNode pre = null;
    10         TreeNode cur = root;
    11         Stack<TreeNode> stack = new Stack<TreeNode>();
    12         while (cur != null || !stack.isEmpty()) {
    13             if (cur != null) {
    14                 stack.push(cur);
    15                 cur = cur.left;
    16             } else {
    17                 cur = stack.pop();
    18                 if (cur.right == null || pre == cur.right) { // won't put back
    19                     list.add(cur.val); // Do manipulation here
    20                     pre = cur;
    21                     cur = null;
    22                 } else {
    23                     stack.push(cur);
    24                     cur = cur.right;
    25                 }
    26             }
    27         }
    28         
    29         return list;
    30     } 
    31 }
     1 // Solution 2. Morris Traversal
     2 // Time Complexity : O(n)
     3 // Space Complexity : O(1)
     4 public class Solution {
     5     public List<Integer> postorderTraversal(TreeNode root) {
     6         List<Integer> list = new ArrayList<Integer>();
     7         
     8         TreeNode dump = new TreeNode(-1);
     9         dump.left = root;
    10         
    11         TreeNode cur = dump;
    12         TreeNode pre = null;
    13         while (cur != null) {
    14             if (cur.left != null) {
    15                 pre = cur.left;
    16                 while (pre.right != null && pre.right != cur) {
    17                     pre = pre.right;
    18                 }
    19                 if (pre.right == null) {
    20                     pre.right = cur;
    21                     cur = cur.left;
    22                 } else {
    23                     List<Integer> rev = new ArrayList<Integer>();
    24                     TreeNode tmp = cur.left;
    25                     while (tmp != cur) {
    26                         rev.add(tmp.val);
    27                         tmp = tmp.right;
    28                     }
    29                     if (!rev.isEmpty()) {
    30                         Collections.reverse(rev); // Collections.reverse return void
    31                         list.addAll(rev);
    32                     }
    33                     pre.right = null;
    34                     cur = cur.right;
    35                 } 
    36             } else {
    37                     cur = cur.right;
    38             }
    39         }
    40         return list;
    41     }
    42 }
  • 相关阅读:
    舍不得花钱的心理分析
    DLL编程的导入导出,__declspec(dllimport),__declspec(dllexport)
    浅谈C/C++内存泄漏及其检测工具
    C++多线程编程简单实例
    linux镜像源设置
    Linux基础教程 linux无密码ssh登录设置
    兄弟连教育分享:用CSS实现鼠标悬停提示的方法
    PHP基础教程 PHP的页面缓冲处理机制
    Linux基础教程 linux下cat 命令使用详解
    PHP基础教程 php 网络上关于设计模式一些总结
  • 原文地址:https://www.cnblogs.com/joycelee/p/5649725.html
Copyright © 2011-2022 走看看