Tracking right most node


  • 0
    I

    slight modification of level order traversal
    tracking the right most node

    def rightSideView(self, root):
        if not root:
            return []
        
        res = []
        queue = [root]
        right_most = root
        while queue:
            curr = queue.pop(0)
            if curr.left:
                queue.append(curr.left)
            if curr.right:
                queue.append(curr.right)
            if curr == right_most:
                res.append(curr.val)
                if queue:
                    right_most = queue[-1]
        return res

Log in to reply
 

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