Python iterative accepted solution with stack


  • 0
    class Solution(object):
        def findBottomLeftValue(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            stack = [root]
            while len(stack) != 0:
                temp = []
                lm = stack[0].val
                while len(stack) != 0:
                    node = stack.pop()
                    if node.right is not None:
                        temp.append(node.right)
                    if node.left is not None:
                        temp.append(node.left)
                if len(temp) == 0:
                    return lm
                stack = temp[::-1]

Log in to reply
 

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