Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- """
- @param start: The start of the edges set
- @param end: The end of the edges set
- @return: Return the subtree count
- """
- def getSubtreeCount(self, start, end):
- # Write your code here
- children_dict = {}
- real_root = start[0]
- for node_index in range(len(start)):
- parent_node = start[node_index]
- child_node = end[node_index]
- if parent_node not in children_dict:
- children_dict[parent_node] = [child_node]
- else:
- children_dict[parent_node].append(child_node)
- def subtree_count(root_node, children_dict):
- if root_node not in children_dict:
- return [1, 0]
- else:
- non_root_tree_num = 0
- root_tree_num = 1
- for children_node in children_dict[root_node]:
- child_root_tree_num, child_non_root_tree_num = subtree_count(children_node, children_dict)
- non_root_tree_num += (child_non_root_tree_num + child_root_tree_num)
- root_tree_num *= (child_root_tree_num + 1)
- return root_tree_num, non_root_tree_num
- output = subtree_count(real_root, children_dict)
- return (output[0] + output[1]) % 10000007
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement