Python straightforward BFS


  • 3
    Z
    def rightSideView(self, root):
        res, nxtL= [], [root] if root else []
        while nxtL:
            res.append(nxtL[-1].val) # right most val as to output
            curL, nxtL = nxtL, []
            for i in curL: # build the next level
                if i.left: nxtL.append(i.left)
                if i.right: nxtL.append(i.right)
        return res

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.