-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGlobalVar.py
More file actions
78 lines (51 loc) · 1.49 KB
/
Copy pathGlobalVar.py
File metadata and controls
78 lines (51 loc) · 1.49 KB
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
from collections import defaultdict
class TreeNode:
def __init__(self, val):
self.childs = []
self.data = val
def buildTree(A, E):
nodeMap = {}
edgeMap = defaultdict(list)
# Store index for each node
for index in range(len(A)):
node = TreeNode(A[index])
nodeMap[index] = node
# Each edge will contain index mapp [0-1,0-2,0-3,0-4]
for edge in E:
edgeMap[edge[0]].append(nodeMap[edge[1]])
print("edgemap", edgeMap)
print("nodemap", nodeMap)
for keys in edgeMap:
print(keys)
root = nodeMap[keys]
print(root.data)
for child in edgeMap[keys]:
root.childs.append(child)
return getUniValuePath(nodeMap[0])
longestPath = 0
def getUniValuePath(root):
def getLongPath(root):
global longestPath
if root is None:
return 0
sum = 0
pathLength = 0
for child in root.childs:
childPAth = getLongPath(child)
childPAth = childPAth + 1 if root.data == child.data else 0
sum += childPAth
pathLength = max(pathLength, childPAth)
longestPath = max(longestPath, sum)
return pathLength
getLongPath(root)
return longestPath
path = 10
def checkGlobal():
global path
def check():
global path
path += 1
print(path)
check()
# checkGlobal()
print("longest path is", buildTree([1, 4, 5, 4, 4, 5], [[0, 1], [0, 2], [1, 3], [1, 4], [2, 5]]))