-
98. Validate Binary Search TreeAlgorithm/java tip 2021. 3. 14. 17:32
leetcode.com/problems/validate-binary-search-tree/
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); } }
'Algorithm > java tip' 카테고리의 다른 글
1557. Minimum Number of Vertices to Reach All Nodes (0) 2021.03.20 797. All Paths From Source to Target (0) 2021.03.20 21. Merge Two Sorted Lists (0) 2021.03.13 530. Minimum Absolute Difference in BST (0) 2021.03.13 589. N-ary Tree Preorder Traversal (0) 2021.03.13