✔️✔️Beat 100% User | 2 Method | C++/Java/Python | with Explanation
Please upvote if you find the solution helpful.  \n\nMethod 1\n## Explanation\nThe code finds the leftmost value of the bottom row in a binary tree using a brea
Please **upvote** if you find the solution helpful.  \n![image](https://assets.leetcode.com/users/images/c6f79057-5758-4fab-bf52-38143d7b11ab_1709078704.750986.png)\n**Method 1**\n## Explanation\nThe code finds the leftmost value of the bottom row in a binary tree using a breadth-first search (BFS) approach.\n\n1. Declare an integer variable \'last\' to store the leftmost value.\n2. Initialize a queue \'q\' of TreeNode pointers and push the root node into the queue.\n3. While the queue is not empty:\n   a. Get the size of the queue to determine the number of elements at the current level.\n   b. Iterate through each element at the current level:\n      i. Pop the element from the queue and store it in \'curr\'.\n      ii. If it\'s the first element at the current level (i == 0), update \'last\' with the value of the current node (curr->val).\n      iii. Push the left and right child nodes of \'curr\' into the queue.\n   c. Move to the next level.\n\n**Time Complexity: O(N)**\n**Space Complexity: O(w)**   `w is the maximum width of the tree`\n**C++**\n```\nclass Solution {\npublic:\n    int findBottomLeftValue(TreeNode* root) {\n        int last=0;\n        queue<TreeNode*> q;\n        q.push(root);\n        while(!q.empty())\n        {\n            int count=q.size();\n            for(int i=0;i<count;i++)\n            {\n                TreeNode* curr=q.front();\n                q.pop();\n                if(i==0)\n                    last=curr->val;     //last leftMost val\n                if(curr->left)\n                    q.push(curr->left);\n                if(curr->right)\n                    q.push(curr->right);\n            }\n        }\n        return last;\n    }\n};\n```\n**Python**\n```\nclass Solution:\n    def findBottomLeftValue(self, root):\n        last = 0\n        q = deque([root])\n\n        while q:\n            count = len(q)\n            for i in range(count):\n                curr = q.popleft()\n                if i == 0:\n                    last = curr.val  # last leftMost val\n                if curr.left:\n                    q.append(curr.left)\n                if curr.right:\n                    q.append(curr.right)\n\n        return last\n```\n**Java**\n```\npublic class Solution {\n    public int findBottomLeftValue(TreeNode root) {\n        int last = 0;\n        Queue<TreeNode> q = new LinkedList<>();\n        q.add(root);\n\n        while (!q.isEmpty()) {\n            int count = q.size();\n            for (int i = 0; i < count; i++) {\n                TreeNode curr = q.poll();\n                if (i == 0)\n                    last = curr.val;  // last leftMost val\n                if (curr.left != null)\n                    q.add(curr.left);\n                if (curr.right != null)\n                    q.add(curr.right);\n            }\n        }\n        return last;\n    }\n}\n```\n**Method 2**\n## Explanation\nThe code recursively finds the value of the bottom-left node in a binary tree, tracking the current depth and leftmost value at each level.\n1. Parameters: Accepts `root` (current node) and `currD` (current depth) as parameters.\n2. Base Case: Checks if the current node (`root`) is NULL. Returns from the function if the node is NULL.\n3. Depth Update: Checks if the current depth (`currD`) is greater than the previous maximum depth. If true, updates both `depth` and `left` variables with values corresponding to the new level.\n4. Recursive Calls: Regardless of depth updates, recursively calls the function on both left and right subtrees. Increments `currD` by 1 for each recursive call.\n5. Result: After all recursive calls finish, returns the leftmost value (`left`) and its level (`depth`).\n\n**Time Complexity: O(N)**\n**Space Complexity: O(w)**   `w is the maximum width of the tree`\n**C++**\n```\nclass Solution {\npublic:\n    int findBottomLeftValue(TreeNode* root) {\n        int depth = 0;\n        int leftmost = 0;\n        help(root, 1, depth, leftmost);\n        return leftmost;\n    }\n\nprivate:\n    void help(TreeNode* node, int currD, int& depth, int& leftmost) {\n       if(node==NULL){\n           return;\n       }\n\n        // If we have reached a new level\n        if (currD > depth) {\n            depth = currD;\n            leftmost = node->val;  \n        }\n\n        // Explore left and right subtrees\n        help(node->left, currD + 1, depth, leftmost);\n        help(node->right, currD + 1, depth, leftmost);\n    }\n};\n```\n**Python**\n```\nclass Solution:\n    def findBottomLeftValue(self, root):\n        result = [0, 0]\n        self.help(root, 1, result)\n        return result[1]\n\n    def help(self, node, currD, result):\n        if node is None:\n            return\n\n        # If we have reached a new level\n        if currD > result[0]:\n            result[0] = currD\n            result[1] = node.val\n\n        # Explore left and right subtrees\n        self.help(node.left, currD + 1, result)\n        self.help(node.right, currD + 1, result)\n\n```\n**Java**\n```\nclass Solution {\n    public int findBottomLeftValue(TreeNode root) {\n        int[] result = new int[]{0, 0};\n        help(root, 1, result);\n        return result[1];\n    }\n\n    private void help(TreeNode node, int currD, int[] result) {\n        if (node == null) {\n            return;\n        }\n\n        // If we have reached a new level\n        if (currD > result[0]) {\n            result[0] = currD;\n            result[1] = node.val;\n        }\n\n        // Explore left and right subtrees\n        help(node.left, currD + 1, result);\n        help(node.right, currD + 1, result);\n    }\n}\n```\n\n![image](https://assets.leetcode.com/users/images/356929c7-5780-45f4-8eb5-3299da5725d8_1707009629.3697278.jpeg)
['Tree', 'Depth-First Search', 'Breadth-First Search', 'C', 'Python', 'Java']