g0001_0100.s0098_validate_binary_search_tree.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g0001_0100.s0098_validate_binary_search_tree;
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
// #Binary_Search_Tree #Data_Structure_I_Day_14_Tree #Level_1_Day_8_Binary_Search_Tree
// #Udemy_Tree_Stack_Queue #Big_O_Time_O(N)_Space_O(log(N))
// #2024_11_13_Time_0_ms_(100.00%)_Space_42.9_MB_(95.84%)
import com_github_leetcode.TreeNode;
/*
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
return solve(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
// we will send a valid range and check whether the root lies in the range
// and update the range for the subtrees
private boolean solve(TreeNode root, long left, long right) {
if (root == null) {
return true;
}
if (root.val <= left || root.val >= right) {
return false;
}
return solve(root.left, left, root.val) && solve(root.right, root.val, right);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy