-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree Constructor.py
36 lines (27 loc) · 1004 Bytes
/
Tree Constructor.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
def TreeConstructor(strArr):
# Use a dictionary to represent the tree. Key is the parent node,
# values are child nodes.
tree = dict()
# For the convenience of counting root nodes, create a list of child nodes
child_nodes = []
for pair in strArr:
child, parent = [x.strip(",()") for x in pair.split(",")]
if parent in tree:
if (len(tree[parent]) == 2):
# If there's already 2 children, you can't add another, as that wouldn't make
# it a proper binary tree
return "false"
tree[parent].append(child)
else:
# Key doesn't exist, so add it
tree[parent] = [child]
child_nodes.append(child)
root_count = 0
# Count the number of root nodes -- the nodes who do not have a parent (and thus don't appear
# in the list of childs)
for node in tree:
if node not in child_nodes:
root_count += 1
return "true" if (root_count == 1) else "false"
# keep this function call here
print(TreeConstructor(input()))