You are viewing a single comment's thread. Return to all comments →
from collections import deque def topView(root): result = {} bfs = deque() bfs.append((root, 0)) while bfs: node, distance = bfs.popleft() if distance not in result: result[distance] = node.info if node.left: bfs.append((node.left, distance - 1)) if node.right: bfs.append((node.right, distance + 1)) result = dict(sorted(result.items())) print(" ".join(map(str, result.values())))
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 →