-
Notifications
You must be signed in to change notification settings - Fork 8
/
youngestCommonAncestor.py
79 lines (59 loc) · 2.87 KB
/
youngestCommonAncestor.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# This is an input class. Do not edit.
class AncestralTree:
def __init__(self, name):
self.name = name
self.ancestor = None
def getYoungestCommonAncestor(topAncestor, descendantOne, descendantTwo):
return getYoungestCommonAncestorHelper(topAncestor, descendantOne, descendantTwo)
def getYoungestCommonAncestorHelper(topAncestor, descendantOne, descendantTwo):
depthOfDescendantOne = findDepthOfAncestralTree(descendantOne)
depthOfDescendantTwo = findDepthOfAncestralTree(descendantTwo)
if depthOfDescendantOne > depthOfDescendantTwo:
possibleAncestorAtLevel = traverseUntilDepthdiff(
descendantOne, getAbsDiffOfDepth(depthOfDescendantOne, depthOfDescendantTwo))
return grabAncestor(possibleAncestorAtLevel, descendantTwo)
else:
possibleAncestorAtLevel = traverseUntilDepthdiff(
descendantTwo, getAbsDiffOfDepth(depthOfDescendantOne, depthOfDescendantTwo))
return grabAncestor(possibleAncestorAtLevel, descendantOne)
def grabAncestor(descendantOne, descendantTwo):
if descendantOne is not descendantTwo:
return grabAncestor(descendantOne.ancestor, descendantTwo.ancestor)
return descendantOne
def traverseUntilDepthdiff(tree: AncestralTree, diff, currentLevel=0):
if diff == currentLevel:
return tree
return traverseUntilDepthdiff(tree.ancestor, diff, currentLevel+1)
def getAbsDiffOfDepth(x, y):
return abs(x-y)
def findDepthOfAncestralTree(tree, depth=0):
# if we still have an ancestor move forward else return depth of tree
if tree.ancestor is not None:
return findDepthOfAncestralTree(tree.ancestor, depth+1)
else:
return depth
# class AncestralTree:
# def __init__(self, name):
# self.name = name
# self.ancestor = None
# def getYoungestCommonAncestor(topAncestor, descendantOne, descendantTwo):
# depthOfDescendantOne = findDepthOfAncestralTree(descendantOne)
# depthOfDescendantTwo = findDepthOfAncestralTree(descendantTwo)
# if depthOfDescendantOne > depthOfDescendantTwo:
# return backtrackAncestralTree(descendantOne, descendantTwo, depthOfDescendantOne-depthOfDescendantTwo)
# else:
# return backtrackAncestralTree(descendantTwo, descendantOne, depthOfDescendantTwo-depthOfDescendantOne)
# def backtrackAncestralTree(lowerDescendant, higherDescendant, diff):
# while diff > 0:
# lowerDescendant = lowerDescendant.ancestor
# diff -= 1
# while lowerDescendant != higherDescendant:
# lowerDescendant = lowerDescendant.ancestor
# higherDescendant = higherDescendant.ancestor
# return lowerDescendant
# def findDepthOfAncestralTree(tree, depth=0):
# # if we still have an ancestor move forward else return depth of tree
# if tree.ancestor is not None:
# return findDepthOfAncestralTree(tree.ancestor, depth+1)
# else:
# return depth