-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcousins-in-binary-tree.go
More file actions
40 lines (36 loc) · 890 Bytes
/
cousins-in-binary-tree.go
File metadata and controls
40 lines (36 loc) · 890 Bytes
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
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
package main
func isCousins(root *TreeNode, x int, y int) bool {
var dfs func(node *TreeNode, x int, y int) (int, bool)
dfs = func(node *TreeNode, x int, y int) (int, bool) {
if node == nil {
return 0, false
}
if node.Val == x || node.Val == y {
return 1, false
}
left_count, left_cousins := dfs(node.Left, x, y)
right_count, right_cousins := dfs(node.Right, x, y)
if left_count != 0 && right_count != 0 {
if left_count == right_count && left_count > 1 {
return 0, true
} else {
return 0, false
}
} else if left_count != 0 {
return left_count + 1, false
} else if right_count != 0 {
return right_count + 1, false
}
return 0, left_cousins || right_cousins
}
_, result := dfs(root, x, y)
return result
}