Algorithm/java tip
98. Validate Binary Search Tree
개구리는 개꿀개꿀
2021. 3. 14. 17:32
leetcode.com/problems/validate-binary-search-tree/
Validate Binary Search Tree - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution {
public boolean isValidBST(TreeNode root) {
return isValidBST(root, false, false, 0, 0);
}
public boolean isValidBST(TreeNode root, boolean isLower, boolean isUpper, int lower, int upper) {
if(root == null) return true;
// 현재 노두가 lower/upper 를 침범하지 않는가
if(isLower && root.val <= lower) {
return false;
}
if(isUpper && root.val >= upper) {
return false;
}
return isValidBST(root.left, isLower, true, lower, root.val)
&& isValidBST(root.right, true, isUpper, root.val, upper);
}
}
Learn one iterative inorder traversal, apply it to multiple tree questions (Java Solution) - LeetCode Discuss
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com