We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
queue = deque([(root, 0)])
while queue:
node, hd = queue.popleft()
if hd not in hd_map:
hd_map[hd] = node.info
if node.left:
queue.append((node.left, hd - 1))
if node.right:
queue.append((node.right, hd + 1))
print(" ".join(str(hd_map[hd]) for hd in sorted(hd_map)))
Cookie support is required to access HackerRank
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 →
from collections import deque def topView(root): if(root==None): return 0 hd_map = {}