-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.scala
executable file
·60 lines (50 loc) · 1.35 KB
/
Solution.scala
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
60
package LowestCommonAncestor
class TreeNode(var _value: Int, var _left: TreeNode = null, var _right: TreeNode = null) {
var value: Int = _value
var left: TreeNode = _left
var right: TreeNode = _right
}
// https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/
object Solution {
def lowestCommonAncestor(root: TreeNode, p: TreeNode, q: TreeNode): TreeNode = {
def getAncestors(root: TreeNode, p: TreeNode, accList: List[TreeNode] = List()): List[TreeNode] = {
if (root == null) List()
else if (root == p) p :: accList
else
getAncestors(root.left, p, root :: accList) ++
getAncestors(root.right, p, root :: accList)
}
val ANCESTORS_P: List[TreeNode] = getAncestors(root, p)
val ANCESTORS_Q: List[TreeNode] = getAncestors(root, q)
ANCESTORS_P
.find(
node =>
ANCESTORS_Q
.contains(node)
) match {
case Some(node) => node
case None => throw new IllegalArgumentException("[ERROR]: No common ancestor.")
}
}
}
object Maine extends App {
val root: TreeNode =
new TreeNode(
3,
new TreeNode(
5,
new TreeNode(6),
new TreeNode(
2,
new TreeNode(7),
new TreeNode(4)
)
),
new TreeNode(
1,
new TreeNode(0),
new TreeNode(8)
)
)
Solution.lowestCommonAncestor(root, root.left, root.right)
}