BFS (Python)


  • 0
    T
    def rightSideView(self, root):
        answer = []
        if not root:
            return answer
        stack = []
        stack.append(root)
        while len(stack) > 0:
            n = len(stack)
            queue = []
            for i in range(n):
                node = stack.pop()
                if i == 0:
                    answer.append(node.val)
                if node.right:
                    stack.insert(0, node.right)
                if node.left:
                    stack.insert(0, node.left)
        return answer

Log in to reply
 

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