Skip to content

Commit 35514f0

Browse files
authored
Merge pull request neetcode-gh#643 from diss1993/ticket/98
Swift | 98. Validate Binary Search Tree
2 parents de186e4 + 24771ca commit 35514f0

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func isValidBST(_ root: TreeNode?) -> Bool {
3+
return isBst(root, min: Int.min, max: Int.max)
4+
}
5+
6+
private func isBst(_ node: TreeNode?, min: Int, max: Int) -> Bool {
7+
if node == nil { return true }
8+
if node!.val <= min || node!.val >= max { return false }
9+
let lc = isBst(node?.left, min: min, max: node!.val)
10+
let rc = isBst(node?.right, min: node!.val, max: max)
11+
return lc && rc
12+
}
13+
}

0 commit comments

Comments
 (0)