Top view of binary tree

def getTopView(root):
res=[]
q=[]
map_str=dict()
temp=[]
if root:
q.append([root,0])
while(q):
size = len(q)
for i in range(size):
front = q.pop(0)
x=front[1]
if x not in map_str:
map_str[x]=front[0].val
if front[0].left:
q.append([front[0].left,x-1])
if front[0].right:
q.append([front[0].right,x+1])
for i in sorted(map_str):
temp.append(map_str[i])
return temp

--

--

--

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Stable Sorting

My experience as A Linux Foundation Intern

Build Android Apps with GitLab CI/CD

Copy, Clone,Create

Monitoring application using Prometheus and grafana

Reliability Snippets: Using SLOs for Delivery Pipeline Guardrails and Gates

GitHub: We Are Save Main Repository Committee

Things I Learned Managing Site Reliability for Some of the World’s Busiest Gambling Sites

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

More from Medium

Structured Query Language (SQL): An Overview

Break Down : GitHub Outage(5th May 2022)

125. Valid Palindrome

What to Do About Big Data from IoT: Knowledge Discovery in Database