• Leetcode-Recover BST


    Two elements of a binary search tree (BST) are swapped by mistake.

    Recover the tree without changing its structure.

    Note:
    A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

    O(n) time and O(1) space solution: Morris Traversal

     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10     
    11 
    12 
    13 public class Solution {
    14     public void recoverTree(TreeNode root) {
    15         if (root==null) return;
    16         
    17         TreeNode pre=null,cur=null,first=null,second=null;
    18         cur = root;
    19         while (cur!=null){
    20             //cur.left is null.
    21             if (cur.left==null){
    22                 if (pre!=null && pre.val>cur.val){
    23                     if (first==null){
    24                         first = pre;
    25                         second = cur;
    26                     } else second = cur;
    27                 }
    28                 pre = cur;
    29                 cur = cur.right;
    30             } else {
    31                 //get predecessor.
    32                 TreeNode temp = getPredecessor(cur);
    33                 if (temp.right==null){
    34                     temp.right=cur;
    35                     cur = cur.left;
    36                 } else {
    37                     if (pre!=null && pre.val>cur.val){
    38                         if (first==null){
    39                             first = pre;
    40                             second = cur;
    41                         } else second = cur;
    42                     }
    43                     temp.right = null;
    44                     pre = cur;
    45                     cur = cur.right;
    46                 }
    47             }
    48         }
    49         
    50         if (first==null) return;
    51         
    52         int temp = first.val;
    53         first.val = second.val;
    54         second.val = temp;
    55         
    56         return;
    57     }
    58     
    59     public TreeNode getPredecessor(TreeNode cur){
    60         TreeNode pre = cur.left;
    61         while (pre.right!=null && pre.right!=cur){
    62             pre = pre.right;
    63         }
    64         
    65         return pre;
    66     }
    67 }

    O(log(n)) space solution:

     1 /**
     2  * Definition for binary tree
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Result {
    11     TreeNode pre;
    12     TreeNode first;
    13     TreeNode second;
    14     Result() {
    15         pre = first = second = null;
    16     }
    17 }
    18 
    19 public class Solution {
    20     public void recoverTree(TreeNode root) {
    21         Result res = new Result();
    22         recoverTreeRecur(root,res);
    23         if (res.first!=null && res.second!=null){
    24             int temp = res.first.val;
    25             res.first.val = res.second.val;
    26             res.second.val = temp;
    27         }
    28     }
    29 
    30     public void recoverTreeRecur(TreeNode cur, Result res){
    31         if (cur==null)
    32             return;
    33 
    34         recoverTreeRecur(cur.left, res);
    35         if (res.pre==null) res.pre = cur;
    36         else if (res.pre.val>cur.val){
    37             if (res.first==null)
    38                 res.first = res.pre;
    39             res.second = cur;
    40         }
    41 
    42         res.pre = cur;
    43         recoverTreeRecur(cur.right,res);
    44     }        
    45 }
  • 相关阅读:
    代理模式与Android
    图数据库 Titan 高速入门
    怎样编写支持命令行选项的程序
    协方差的意义
    我所理解的Spring AOP的基本概念
    Google搜索解析
    POJ 3311 Hie with the Pie floyd+状压DP
    JS怎样将拖拉事件与点击事件分离?
    C++语言笔记系列之十二——C++的继承
    Mac下Android配置及unity3d的导出Android
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4084382.html
Copyright © 2020-2023  润新知