Skip to content

Commit 68e21e5

Browse files
author
freemanzhang
committed
refactor implementation
1 parent 724a7ce commit 68e21e5

File tree

1 file changed

+37
-44
lines changed

1 file changed

+37
-44
lines changed

src/binarySearchTree/LargestBSTSubtree.java

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,71 +27,64 @@ Can you figure out ways to solve it with O(n) time complexity?
2727

2828
public class LargestBSTSubtree
2929
{
30-
private class ResultWrapper
30+
private class Wrapper
3131
{
32-
public final boolean isBST;
33-
public final int largestBSTSize;
34-
public final int minValue;
35-
public final int maxValue;
36-
public ResultWrapper( boolean isBST, int largestBSTSize, int minValue, int maxValue )
32+
public boolean isBST;
33+
public int size;
34+
public int lower;
35+
public int upper;
36+
37+
public Wrapper()
3738
{
38-
this.isBST = isBST;
39-
this.largestBSTSize = largestBSTSize;
40-
this.minValue = minValue;
41-
this.maxValue = maxValue;
39+
lower = Integer.MAX_VALUE;
40+
upper = Integer.MIN_VALUE;
41+
isBST = false;
42+
size = 0;
4243
}
4344
}
4445

4546
public int largestBSTSubtree( TreeNode root )
4647
{
47-
ResultWrapper result = largestBSTSubtreeRecurse( root );
48-
return result.largestBSTSize;
48+
if ( root == null )
49+
{
50+
return 0;
51+
}
52+
53+
Wrapper result = helper( root );
54+
return result.size;
4955
}
5056

51-
private ResultWrapper largestBSTSubtreeRecurse( TreeNode root )
57+
private Wrapper helper( TreeNode root )
5258
{
59+
Wrapper result = new Wrapper();
60+
5361
if ( root == null )
5462
{
55-
return new ResultWrapper( false, 0, Integer.MIN_VALUE, Integer.MAX_VALUE );
56-
}
57-
if ( root.left == null
58-
&& root.right == null )
59-
{
60-
return new ResultWrapper( true, 1, root.val, root.val );
63+
result.isBST = true;
64+
return result;
6165
}
6266

63-
ResultWrapper leftResult = largestBSTSubtreeRecurse( root.left );
64-
ResultWrapper rightResult = largestBSTSubtreeRecurse( root.right );
67+
Wrapper leftResult = helper( root.left );
68+
Wrapper rightResult = helper( root.right );
69+
70+
result.lower = Math.min( root.val, leftResult.lower );
71+
result.upper = Math.max( root.val, rightResult.upper );
6572

6673
if ( leftResult.isBST
67-
&& rightResult.isBST )
68-
{
69-
if ( leftResult.maxValue < root.val
70-
&& root.val < rightResult.minValue )
71-
{
72-
return new ResultWrapper( true, leftResult.largestBSTSize + rightResult.largestBSTSize + 1, leftResult.minValue, rightResult.maxValue );
73-
}
74-
else if ( leftResult.largestBSTSize > rightResult.largestBSTSize )
75-
{
76-
return new ResultWrapper( false, leftResult.largestBSTSize, leftResult.minValue, leftResult.maxValue );
77-
}
78-
else
79-
{
80-
return new ResultWrapper( false, rightResult.largestBSTSize, rightResult.minValue, rightResult.maxValue );
81-
}
82-
}
83-
else if ( leftResult.isBST )
74+
&& rightResult.isBST
75+
&& leftResult.upper <= root.val
76+
&& rightResult.lower >= root.val )
8477
{
85-
return new ResultWrapper( false, leftResult.largestBSTSize, leftResult.minValue, leftResult.maxValue );
86-
}
87-
else if ( rightResult.isBST )
88-
{
89-
return new ResultWrapper( false, rightResult.largestBSTSize, rightResult.minValue, rightResult.maxValue );
78+
result.size = leftResult.size + rightResult.size + 1;
79+
result.isBST = true;
9080
}
9181
else
9282
{
93-
return new ResultWrapper( false, 1, root.val, root.val );
83+
result.size = Math.max( leftResult.size, rightResult.size );
84+
result.isBST = false;
9485
}
86+
87+
return result;
9588
}
9689

9790
@Ignore

0 commit comments

Comments
 (0)