-
Notifications
You must be signed in to change notification settings - Fork 0
/
653.go
50 lines (44 loc) · 1.17 KB
/
653.go
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
// Name: Validate Binary Search Tree
// Tags: Binary Search Tree, Depth First Search, Preorder Traversal
// Stats: 16ms-98.87%, 7.4mb-83.62%
// recursion would have been much easier but wanted to practise
// implementation with a queue.
func soln1(root *TreeNode, k int) bool {
if root == nil { return false }
queue := make([]*TreeNode, 0)
queue = append(queue, root)
checked := make(map[int]bool, 0)
for len(queue) > 0 {
curr := queue[0]
queue = queue[1:]
_,in1 := checked[curr.Val]
_,in2 := checked[k-curr.Val]
if in1 != true && in2 != true {
if k-curr.Val == curr.Val {
if curr.Left != nil {
queue = append(queue, curr.Left)
}
if curr.Right != nil {
queue = append(queue, curr.Right)
}
continue
}
t := check(root, k-curr.Val)
if t { return true }
checked[curr.Val] = true
}
if curr.Left != nil {
queue = append(queue, curr.Left)
}
if curr.Right != nil {
queue = append(queue, curr.Right)
}
}
return false
}
func check(root *TreeNode, val int) bool {
if root == nil { return false }
if root.Val == val { return true }
if val > root.Val { return check(root.Right, val) }
return check(root.Left, val)
}