Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution{
- public static int[] largestBSTBT(Node root)
- {
- if (root == null)
- return new int[] { Integer.MAX_VALUE,
- Integer.MIN_VALUE, 0 };
- if (root.left == null && root.right == null)
- return new int[] { root.data, root.data, 1 };
- int[] left = largestBSTBT(root.left);
- int[] right = largestBSTBT(root.right);
- int[] ans = new int[3];
- if ((left[1] < root.data)
- && (right[0] > root.data)) {
- ans[0] = Math.min(left[0], Math.min(right[0], root.data));
- ans[1] = Math.max(right[1], Math.max(left[1], root.data));
- ans[2] = 1 + left[2] + right[2];
- return ans;
- }
- ans[0] = Integer.MIN_VALUE;
- ans[1] = Integer.MAX_VALUE;
- ans[2] = Math.max(left[2], right[2]);
- return ans;
- }
- public static int largestBSTBTutil(Node root)
- {
- return largestBSTBT(root)[2];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement