BFS (Python)

  • 0
    def rightSideView(self, root):
        answer = []
        if not root:
            return answer
        stack = []
        while len(stack) > 0:
            n = len(stack)
            queue = []
            for i in range(n):
                node = stack.pop()
                if i == 0:
                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.