You are viewing a single comment's thread. Return to all comments →
Any suggestion to increase efficiency are appreciated please
def topView(root): from collections import deque if not root: return top_nodes = {} queue = deque([(root, 0)]) while queue: node, hd = queue.popleft() if hd not in top_nodes: top_nodes[hd] = node.info if node.left: queue.append((node.left, hd - 1)) if node.right: queue.append((node.right, hd + 1)) for hd in sorted(top_nodes): print(top_nodes[hd], end=" ") print()
Seems like cookies are disabled on this browser, please enable them to open this website
Tree : Top View
You are viewing a single comment's thread. Return to all comments →
Here's My Python 3 Code
Any suggestion to increase efficiency are appreciated please