Skip to content

Commit 4f7ef36

Browse files
committed
Create 98-Validate-Binary-Search-Tree.cs
1 parent d6e161f commit 4f7ef36

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
public class Solution {
15+
public bool IsValidBST(TreeNode root, int? left = null, int? right = null) {
16+
if(root == null) return true;
17+
18+
if((left != null && root.val <= left) ||
19+
(right != null && root.val >= right))
20+
return false;
21+
22+
return IsValidBST(root.left, left, root.val) &&
23+
IsValidBST(root.right, root.val, right);
24+
}
25+
}

0 commit comments

Comments
 (0)