Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self ,key):
- self.key = key
- self.child = []
- def longestUnivaluePath(root):
- def arrow_length(node):
- global ans
- child_lengths = [ arrow_length(child_node) for child_node in node.child ]
- child_arrows = [ (child_length+1) for child_node, child_length in zip(node.child, child_lengths) if child_node.key == node.key ]
- ans = max(child_arrows + [ans])
- return max(child_arrows) if child_arrows else 0
- arrow_length(root)
- return ans
- root = Node(2)
- child_nodes = [Node(2), Node(1), Node(10), Node(3)]
- root.child = child_nodes
- sub_child_nodes_2 = [Node(2), Node(2)]
- child_nodes[0].child = sub_child_nodes_2
- sub_child_nodes_1 = [Node(2), Node(3)]
- child_nodes[1].child = sub_child_nodes_1
- sub_child_nodes_10 = [Node(2), Node(3)]
- child_nodes[2].child = sub_child_nodes_10
- sub_child_nodes_3 = [Node(2), Node(3)]
- child_nodes[3].child = sub_child_nodes_3
- ans = 0
- print longestUnivaluePath(root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement