Skip to content

Commit 3745274

Browse files
authored
Update README.md
1 parent 3ad031a commit 3745274

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,37 @@ Input: [5,1,4,null,null,3,6]
3131
Output: false
3232
Explanation: The root node's value is 5 but its right child's value is 4.
3333
```
34-
# Implementation :
34+
## Implementation 1 : Recursive
35+
36+
```java
37+
/**
38+
* Definition for a binary tree node.
39+
* public class TreeNode {
40+
* int val;
41+
* TreeNode left;
42+
* TreeNode right;
43+
* TreeNode(int x) { val = x; }
44+
* }
45+
*/
46+
class Solution {
47+
public boolean isValidBST(TreeNode root) {
48+
if(root == null)
49+
return true;
50+
return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
51+
}
52+
53+
private boolean isValidBST(TreeNode node, long min, long max){
54+
if(node == null)
55+
return true;
56+
if(node.val <= min || node.val >= max)
57+
return false;
58+
return isValidBST(node.left, min, node.val) &&
59+
isValidBST(node.right, node.val, max);
60+
}
61+
}
62+
```
63+
64+
## Implementation 2 : Iterative
3565

3666
```java
3767
/**

0 commit comments

Comments
 (0)