513: Solution with step by step explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Initialize a queue with the root node.\n2. Initialize a variable to st
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize a queue with the root node.\n2. Initialize a variable to store the leftmost node value as the value of the root node.\n3. Perform level-order traversal using a while loop until the queue becomes empty.\n4. Get the number of nodes in the current level using the length of the queue.\n5. Traverse all the nodes in the current level using a for loop.\n6. Dequeue the front node from the queue.\n7. Update the leftmost node value if the current node is in the leftmost position of the current level.\n8. Enqueue the left and right child nodes of the current node (if they exist).\n9. After the while loop, return the leftmost node value found in the last level.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def findBottomLeftValue(self, root: TreeNode) -> int:\n        # Initialize a queue with the root node\n        queue = deque([root])\n        # Initialize a variable to store the leftmost node value\n        leftmost_val = root.val\n        \n        # Perform level-order traversal\n        while queue:\n            # Get the number of nodes in the current level\n            level_size = len(queue)\n            \n            # Traverse all the nodes in the current level\n            for i in range(level_size):\n                # Dequeue the front node from the queue\n                node = queue.popleft()\n                \n                # Update the leftmost node value if the current node is in the leftmost position of the current level\n                if i == 0:\n                    leftmost_val = node.val\n                \n                # Enqueue the left and right child nodes of the current node (if they exist)\n                if node.left:\n                    queue.append(node.left)\n                if node.right:\n                    queue.append(node.right)\n        \n        # Return the leftmost node value found in the last level\n        return leftmost_val\n\n```
['Tree', 'Depth-First Search', 'Breadth-First Search', 'Python', 'Python3']