• leetcode--Recover Binary Search Tree


    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?

    solution 1 using O(n) space:

     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 public class Solution {
    11     public void recoverTree(TreeNode root) {
    12          ArrayList<TreeNode> Inorder = InOrderTraversal(root);
    13         int firstViolation = 0;
    14         int secondViolation = 0;
    15         int length = Inorder.size();
    16         if(!Inorder.isEmpty()){
    17             int i = 0;
    18             for(; i <length - 1; ++i){
    19                 if(Inorder.get(i).val > Inorder.get(i + 1).val){
    20                     firstViolation = i;
    21                     break;
    22                 }                    
    23             }
    24             ++i;
    25             for(; i < length - 1; ++i){
    26                 if(Inorder.get(i).val > Inorder.get(i + 1).val){
    27                     secondViolation = i + 1;
    28                     break;
    29                 }                    
    30             }
    31         }
    32         if(secondViolation == 0)
    33             secondViolation = firstViolation + 1;
    34         int temp = Inorder.get(firstViolation).val;
    35         Inorder.get(firstViolation).val = Inorder.get(secondViolation).val;
    36         Inorder.get(secondViolation).val = temp;        
    37     }
    38     public ArrayList<TreeNode> InOrderTraversal(TreeNode root){
    39         ArrayList<TreeNode> InOrder = new ArrayList<TreeNode>();
    40         if(root != null){
    41             if(root.left != null){
    42                 ArrayList<TreeNode> leftChild = InOrderTraversal(root.left);
    43                 InOrder.addAll(leftChild);
    44             }
    45             InOrder.add(root);
    46             if(root.right != null){
    47                 ArrayList<TreeNode> rightChild = InOrderTraversal(root.right);
    48                 InOrder.addAll(rightChild);
    49             }
    50         }
    51         return InOrder;
    52     }
    53 }

    Solution2: using O(1) extra space: 

     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 public class Solution {
    11     public void recoverTree(TreeNode root) {
    12         if(root != null){
    13             inOrder(root);
    14             int temp = firstViolation.val;
    15             firstViolation.val = secondViolation.val;
    16             secondViolation.val = temp;
    17         }
    18     }
    19     
    20     public void inOrder(TreeNode root){
    21         if(root != null){
    22             if(root.left != null)
    23                 inOrder(root.left);
    24             if(previous == null)
    25                 previous = root;
    26             else{
    27                 if(previous.val > root.val){
    28                     if(firstViolation == null)
    29                         firstViolation = previous;
    30                     secondViolation = root;                        
    31                 }
    32                 previous = root;
    33             }            
    34             if(root.right != null)
    35                 inOrder(root.right);
    36         }
    37     }    
    38     TreeNode previous = null;
    39     TreeNode firstViolation = null;
    40     TreeNode secondViolation = null;
    41 }
  • 相关阅读:
    利用Python进行数据分析-Pandas(第六部分-数据聚合与分组运算)
    利用Python进行数据分析-Pandas(第五部分-数据规整:聚合、合并和重塑)
    利用Python进行数据分析-Pandas(第四部分-数据清洗和准备)
    使用VBA从工作表中读图片,以及给工作表中写文件
    利用Python进行数据分析-Pandas(第三部分)
    利用Python进行数据分析-Pandas(第二部分)
    Shape.Type属性名称及对应值列表
    利用Python进行数据分析-Pandas(第一部分)
    下载文件
    github访问很慢的解决办法
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3536156.html
Copyright © 2020-2023  润新知