🌲 Binary Tree Explorer: Mastered JavaScript, Python, Python3, C++ | 100.00% Efficiency Seeker ⚡️
Show your appreciation by clicking the upvote button\n\n\n\n# Intuition\nThe code aims to find and return the leftmost value in the last level of a binary tree.
## Show your appreciation by clicking the upvote button\n![Screen Shot 2024-03-14 at 11.30.19.png](https://assets.leetcode.com/users/images/99fe0b15-b318-4601-af0f-574bc98640ee_1710652249.4421208.png)\n\n\n# Intuition\nThe code aims to find and return the leftmost value in the last level of a binary tree. The use of a deque and a level-order traversal suggests a breadth-first search (BFS) approach to efficiently traverse the tree level by level.\n\n# Approach\n1. Initialize a deque `queue` with the root node and a variable `leftmost_value` to None.\n2. Perform a while loop while the queue is not empty:\n   - Dequeue a node from the left of the deque.\n   - Update `leftmost_value` with the value of the dequeued node.\n   - Enqueue the right child if it exists.\n   - Enqueue the left child if it exists.\n3. Continue the process until all nodes at the last level are processed.\n4. Return the `leftmost_value`.\n\n![Copy of Untitled drawing.png](https://assets.leetcode.com/users/images/21450107-c261-4106-bb8d-1e1606496530_1709138247.0285048.png)\n\n# Complexity\n- Time complexity: $$O(n)$$, where $$n$$ is the number of nodes in the binary tree. Each node is processed once during the level-order traversal.\n- Space complexity: $$O(m)$$, where $$m$$ is the maximum number of nodes at any level in the binary tree. In the worst case, the queue would store all nodes at the maximum level.\n\n![Screen Shot 2024-03-14 at 11.30.19.png](https://assets.leetcode.com/users/images/99fe0b15-b318-4601-af0f-574bc98640ee_1710652249.4421208.png)\n\n```python []\nclass Solution(object):\n    def findBottomLeftValue(self, root):\n        queue = deque([root])\n        leftmost_value = None\n\n        while queue:\n            node = queue.popleft()\n\n            leftmost_value = node.val\n\n            if node.right:\n                queue.append(node.right)\n            if node.left:\n                queue.append(node.left)\n\n        return leftmost_value\n```\n```python3 []\nclass Solution:\n    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:\n        queue = deque([root])\n        leftmost_value = None\n\n        while queue:\n            node = queue.popleft()\n\n            leftmost_value = node.val\n\n            if node.right:\n                queue.append(node.right)\n            if node.left:\n                queue.append(node.left)\n\n        return leftmost_value\n```\n```C++ []\nclass Solution {\npublic:\n    int findBottomLeftValue(TreeNode* root) {\n        queue<TreeNode*> q;\n        q.push(root);\n        int leftmost_value;\n\n        while (!q.empty()) {\n            TreeNode* node = q.front();\n            q.pop();\n\n            leftmost_value = node->val;\n\n            if (node->right) {\n                q.push(node->right);\n            }\n            if (node->left) {\n                q.push(node->left);\n            }\n        }\n\n        return leftmost_value;\n    }\n};\n```\n```JavaScript []\n/**\n * @param {TreeNode} root\n * @return {number}\n */\nvar findBottomLeftValue = function(root) {\n\n    const queue = [root];\n    let leftmostValue;\n\n    while (queue.length > 0) {\n        const node = queue.shift();\n\n        leftmostValue = node.val;\n\n        if (node.right) {\n            queue.push(node.right);\n        }\n        if (node.left) {\n            queue.push(node.left);\n        }\n    }\n\n    return leftmostValue;\n};\n\n```\n\n\n![i_pixian_ai.png](https://assets.leetcode.com/users/images/3b1fafa6-cd15-49b5-82ed-4fdb5e2e3feb_1709119342.411011.png)\n\n## **My solutions**\n\n\uD83D\uDFE2 - $$easy$$  \n\uD83D\uDFE1 - $$medium$$ \n\uD83D\uDD34 - $$hard$$\n\n\uD83D\uDFE1 [17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/solutions/4845532/there-is-an-80-chance-of-being-in-the-interview-full-problem-explanation)\n\uD83D\uDFE1 [22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/solutions/4845742/simple-explanation-with-pictures)\n\uD83D\uDFE1 [39. Combination Sum](https://leetcode.com/problems/combination-sum/solutions/4847482/beat-8292-full-explanation-with-pictures)\n\uD83D\uDFE2 [2540. Minimum Common Value](https://leetcode.com/problems/minimum-common-value/solutions/4845076/beat-9759-full-explanation-with-pictures)\n\uD83D\uDFE2 [3005. Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency/solutions/4839796/beat-8369-full-explanation-with-pictures)\n\uD83D\uDFE2 [3028. Ant on the Boundary](https://leetcode.com/problems/ant-on-the-boundary/solutions/4837433/full-explanation-with-pictures)\n\uD83D\uDFE2 [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/solutions/4834682/beat-10000-full-explanation-with-pictures)\n\uD83D\uDFE1 [1750. Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/solutions/4824224/beat-10000-full-explanation-with-pictures)\n\uD83D\uDFE1 [948. Bag of Tokens](https://leetcode.com/problems/bag-of-tokens/solutions/4818912/beat-10000-full-explanation-with-pictures)\n\uD83D\uDFE1 [19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/solutions/4813340/beat-10000-full-explanation-with-pictures)\n\uD83D\uDFE2 [977. Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/solutions/4807704/square-sorter-python-python3-javascript-c)\n\uD83D\uDFE2 [2864. Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number/solutions/4802402/visual-max-odd-binary-solver-python-python3-javascript-c)\n\uD83D\uDFE1 [1609. Even Odd Tree](https://leetcode.com/problems/even-odd-tree/solutions/4797529/even-odd-tree-validator-python-python3-javascript-c)\n\uD83D\uDFE2 [9. Palindrome Number](https://leetcode.com/problems/palindrome-number/solutions/4795373/why-not-1-line-of-code-python-python3-c-everyone-can-understand)\n\uD83D\uDFE1 [513. Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/solutions/4792022/binary-tree-explorer-mastered-javascript-python-python3-c-10000-efficiency-seeker)\n\uD83D\uDFE2 [1. Two Sum](https://leetcode.com/problems/two-sum/solutions/4791305/5-methods-python-c-python3-from-easy-to-difficult)\n\uD83D\uDFE2 [543. Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/solutions/4787634/surpassing-9793-memory-magician-excelling-at-9723)\n[More...](https://leetcode.com/DevOgabek/)\n\n
['Tree', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'Python', 'C++', 'Python3', 'JavaScript']