Advertisement
unknown_0711

Untitled

Dec 28th, 2022
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. class Solution{
  2. public static int[] largestBSTBT(Node root)
  3. {
  4. if (root == null)
  5. return new int[] { Integer.MAX_VALUE,
  6. Integer.MIN_VALUE, 0 };
  7. if (root.left == null && root.right == null)
  8. return new int[] { root.data, root.data, 1 };
  9.  
  10.  
  11. int[] left = largestBSTBT(root.left);
  12. int[] right = largestBSTBT(root.right);
  13.  
  14.  
  15. int[] ans = new int[3];
  16.  
  17.  
  18. if ((left[1] < root.data)
  19. && (right[0] > root.data)) {
  20. ans[0] = Math.min(left[0], Math.min(right[0], root.data));
  21. ans[1] = Math.max(right[1], Math.max(left[1], root.data));
  22. ans[2] = 1 + left[2] + right[2];
  23. return ans;
  24. }
  25. ans[0] = Integer.MIN_VALUE;
  26. ans[1] = Integer.MAX_VALUE;
  27. ans[2] = Math.max(left[2], right[2]);
  28.  
  29. return ans;
  30. }
  31.  
  32. public static int largestBSTBTutil(Node root)
  33. {
  34.  
  35. return largestBSTBT(root)[2];
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement