|
| 1 | +// public class Solution |
| 2 | +// { |
| 3 | +// public static int largestBSTSubtree(BinaryTreeNode<Integer> root) |
| 4 | +// { |
| 5 | +// Pair<Pair<Integer,Integer>,Pair<Boolean,Integer>> ans=helper(root);// integer=min, integer=max,isBst,height of highest subtree... |
| 6 | +// return ans.second.second; |
| 7 | +// } |
| 8 | +// private static Pair<Pair<Integer,Integer>,Pair<Boolean,Integer>> helper(BinaryTreeNode<Integer> root){ |
| 9 | +// if(root==null) |
| 10 | +// { Pair<Integer,Integer> p1=new Pair<>(Integer.MAX_VALUE,Integer.MIN_VALUE); |
| 11 | +// Pair<Boolean,Integer> p2=new Pair<>(true,0); |
| 12 | +// Pair<Pair<Integer,Integer>,Pair<Boolean,Integer>> ans=new Pair<>(p1,p2); |
| 13 | +// return ans; |
| 14 | +// } |
| 15 | +// Pair<Pair<Integer,Integer>,Pair<Boolean,Integer>> l=helper(root.left); |
| 16 | +// Pair<Pair<Integer,Integer>,Pair<Boolean,Integer>> r=helper(root.right); |
| 17 | +// Pair<Pair<Integer,Integer>,Pair<Boolean,Integer>> ans; |
| 18 | +// if(l.second.first==true && r.second.first==true){ |
| 19 | +// if(root.data>l.first.second && root.data<=r.first.first){ |
| 20 | +// Pair<Integer,Integer> p1=new Pair<>(l.first.first,r.first.second); |
| 21 | +// Pair<Boolean,Integer> p2=new Pair<>(true,1+Math.max(l.second.second,r.second.second)); |
| 22 | +// ans=new Pair<>(p1,p2); |
| 23 | +// } |
| 24 | +// else { |
| 25 | +// Pair<Integer,Integer> p1=new Pair(l.first.first,r.first.second); |
| 26 | +// Pair<Boolean,Integer> p2=new Pair<>(false,Math.max(l.second.second,r.second.second)); |
| 27 | +// ans=new Pair<>(p1,p2); |
| 28 | +// }} |
| 29 | +// // else if(l.second.first==true) |
| 30 | +// // { |
| 31 | +// // Pair<Pair<Integer,Integer>,Pair<Boolean,Integer>> ans=new Pair<>(l.first.first,r.first.second,false,Math.max(l.second.second,r.second.second)); |
| 32 | +// // } |
| 33 | +// // else if(r.second.second==true){ |
| 34 | +// // Pair<Pair<Integer,Integer>,Pair<Boolean,Integer>> ans=new Pair<>(l.first.first,r.first.second,false,Math.max(l.second.second,r.second.second)); |
| 35 | +// // } |
| 36 | +// else{ |
| 37 | +// Pair<Integer,Integer> p1=new Pair(l.first.first,r.first.second); |
| 38 | +// Pair<Boolean,Integer> p2=new Pair(false,Math.max(l.second.second,r.second.second)); |
| 39 | +// ans=new Pair<>(p1,p2); } |
| 40 | +// return ans; |
| 41 | + |
| 42 | +// } |
| 43 | +// } |
| 44 | + |
| 45 | +// class Pair<T,U>{ |
| 46 | +// T first; |
| 47 | +// U second; |
| 48 | +// public Pair(T first,U second){ |
| 49 | +// this.first=first; |
| 50 | +// this.second=second; |
| 51 | +// } |
| 52 | +// } |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +class Pair{ |
| 90 | + int min; |
| 91 | + int max; |
| 92 | + boolean isBST; |
| 93 | + int height; |
| 94 | +} |
| 95 | + |
| 96 | +public class Solution { |
| 97 | + |
| 98 | +/* Binary Tree Node class |
| 99 | + * |
| 100 | + * class BinaryTreeNode<T> { |
| 101 | + T data; |
| 102 | + BinaryTreeNode<T> left; |
| 103 | + BinaryTreeNode<T> right; |
| 104 | + |
| 105 | + public BinaryTreeNode(T data) { |
| 106 | + this.data = data; |
| 107 | + } |
| 108 | + } |
| 109 | +*/ |
| 110 | + |
| 111 | + public static int largestBSTSubtree(BinaryTreeNode<Integer> root) { |
| 112 | + |
| 113 | + Pair ans = largestBSThelper(root); |
| 114 | + return ans.height; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + public static Pair largestBSThelper(BinaryTreeNode<Integer> root){ |
| 119 | + if(root == null){ |
| 120 | + Pair output = new Pair(); |
| 121 | + output.max = Integer.MIN_VALUE; |
| 122 | + output.min = Integer.MAX_VALUE; |
| 123 | + output.isBST = true; |
| 124 | + output.height = 0; |
| 125 | + return output; |
| 126 | + } |
| 127 | + |
| 128 | + Pair left = largestBSThelper(root.left); |
| 129 | + Pair right = largestBSThelper(root.right); |
| 130 | + |
| 131 | + Pair output = new Pair(); |
| 132 | + |
| 133 | + int min = Math.min(root.data,Math.min(left.min,right.min)); |
| 134 | + int max = Math.max(root.data,Math.max(left.max,right.max)); |
| 135 | + |
| 136 | + output.min = min; |
| 137 | + output.max = max; |
| 138 | + |
| 139 | + output.isBST = left.max < root.data && right.min > root.data |
| 140 | + && left.isBST && right.isBST; |
| 141 | + |
| 142 | + if(output.isBST){ |
| 143 | + output.height = Math.max(left.height,right.height) + 1; |
| 144 | + }else{ |
| 145 | + output.height = Math.max(left.height,right.height); |
| 146 | + } |
| 147 | + |
| 148 | + return output; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments