-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path98.go
More file actions
59 lines (54 loc) · 1.2 KB
/
98.go
File metadata and controls
59 lines (54 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package easy_collection
import "math"
/**
* 98. Validate Binary Search Tree
* <p>
* Given a binary tree, determine if it is a valid binary search tree (BST).
* <p>
* Assume a BST is defined as follows:
* <p>
* The left subtree of a node contains only nodes with keys less than the node's key.
* The right subtree of a node contains only nodes with keys greater than the node's key.
* Both the left and right subtrees must also be binary search trees.
* <p>
* Example 1:
* <p>
* 2
* / \
* 1 3
* <p>
* Input: [2,1,3]
* Output: true
* <p>
* Example 2:
* <p>
* 5
* / \
* 1 4
* / \
* 3 6
* <p>
* Input: [5,1,4,null,null,3,6]
* Output: false
* Explanation: The root node's value is 5 but its right child's value is 4.
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isValidBST(root *TreeNode) bool {
return helper98(root, math.MinInt64, math.MaxInt64)
}
func helper98(root *TreeNode, lower, upper int) bool {
if root == nil {
return true
}
if root.Val <= lower || root.Val >= upper {
return false
}
return helper98(root.Left, lower, root.Val) && helper98(root.Right, root.Val, upper)
}