-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_lca.go
56 lines (51 loc) · 1.39 KB
/
tree_lca.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
51
52
53
54
55
56
package problem0236
import . "leetcodedaily/helpers/binarytree"
/*
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia:
“The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants
(where we allow a node to be a descendant of itself).”
*/
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
_, _, res := lowestCommonDFS(root, p, q, false)
return res
}
func lowestCommonDFS(root, p, q *TreeNode, halfDone bool) (found, done bool, res *TreeNode) {
var childFound, childDone bool
var childRes *TreeNode
var selfFound = root.Val == p.Val || root.Val == q.Val
if selfFound && halfDone {
return true, false, nil
}
if root.Left == nil && root.Right == nil {
return selfFound, false, nil
}
if root.Left != nil {
childFound, childDone, childRes = lowestCommonDFS(root.Left, p, q, selfFound)
}
if childDone {
return true, true, childRes
}
if childFound {
if selfFound {
return true, true, root
} else {
selfFound = true
childFound = false
}
}
if root.Right != nil {
childFound, childDone, childRes = lowestCommonDFS(root.Right, p, q, selfFound)
}
if childDone {
return true, true, childRes
}
if childFound {
if selfFound {
return true, true, root
} else {
selfFound = true
}
}
return selfFound, false, nil
}