Created
January 15, 2023 21:15
-
-
Save RakhmedovRS/cf16254b30ce548661334742968d9193 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public boolean isValidBST(TreeNode root) | |
| { | |
| Integer[] prev = new Integer[1]; | |
| return isValidBST(root, prev); | |
| } | |
| public boolean isValidBST(TreeNode root, Integer[] prev) | |
| { | |
| boolean left = true; | |
| if (root.left != null) | |
| { | |
| left = isValidBST(root.left, prev); | |
| } | |
| if (prev[0] != null && prev[0] >= root.val) | |
| { | |
| return false; | |
| } | |
| prev[0] = root.val; | |
| boolean right = true; | |
| if (root.right != null) | |
| { | |
| right = isValidBST(root.right, prev); | |
| } | |
| return left && right; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment