Short python solution, level order traversal


  • 0
    Y
    class Solution:
    # @param {TreeNode} root
    # @return {integer[]}
    def rightSideView(self, root):
        cur=[]
        next=[]
        result=[]
        if root is None:
            return []
        cur.append(root)
        result.append(root.val)
        while len(cur)>0:
            for x in cur:
                if x.left:
                    next.append(x.left)
                if x.right:
                    next.append(x.right)
            cur = next
            next = []
            if len(cur)>0:
                result.append(cur[-1].val)
        return result

Log in to reply
 

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