Tree Top View of Binary Tree HackerRank Challenge - Solution Coded in Python

preview_player
Показать описание
In this challenge we would write a program to find the heigh of a binary tree HackerRank Solution Coded in Python

🔥 This is a Complete Tutorial Series on Binary Trees

Solutions to Other Binary Tree Challenges

🔥 Python Tutorials here

🔥 Data Science Tutorial

My websites

Pleas join us at International Computer Programmers here:

Feel free to connect with me

Your support can help me improve my content:
Рекомендации по теме
Комментарии
Автор

def topView(root):
d = {}

def traverse(node, height=0, deep=0):
if(height in d):
if(deep < d[height][1]):
d[height]=[node, deep]
else:
d[height]=[node, deep]
if node.left:
traverse(node.left, height-1, deep+1)
if node.right:
traverse(node.right, height+1, deep+1)

traverse(root)
for k in sorted(d):
print(d[k][0], end=" ")

yacob