199. Binary Tree Right Side View

class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
res=[]
q=[]
if root:
q.append([root,0])
while(q):
size = len(q)
for i in range(size):
front = q.pop(0)
x=front[1]
if front[0].left:
q.append([front[0].left,x-1])
if front[0].right:
q.append([front[0].right,x+1])
if(i==size-1):
res.append(front[0].val)
return res
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
res=[]
self.rec(root,0,res)
return res

def rec(self,root,level,res):
if not root:
return
if len(res)==level:
res.append(root.val)
self.rec(root.right,level+1,res)
self.rec(root.left,level+1,res)

--

--

--

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

Finding an Affine Transform the Traditional Way with three 2D point correspondences in Swift

Using Lottie Animations in iOS

How to show and hide content with DisclosureGroup using SwiftUI

SwiftUI and UIKit interoperability

UserDefaults Unit testing in iOS

SwiftUI Tutorial — Finding a Route and Directions

5 things every iOS developer learning Android should know

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

“ Experience creating a high level architecture and how it helps to see an overall picture of the…

Debouncing vs Throttling

Backward compatibility — The Daily PPILL #33

Netflix and No chill