C++ and Python3 || DFS || Simple and Optimal
\n\n# Code\nC++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     T
> ![image.png](https://assets.leetcode.com/users/images/97d7bbf2-d6ff-4f6a-a2cc-e76b0a4ba2ff_1709088064.2295382.png)\n\n# Code\n```C++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    void dfs(TreeNode* root, int currDepth, int &maxDepth, int &ans) {\n        if(!root) return;\n        if(currDepth > maxDepth) {\n            ans = root->val;\n            maxDepth = currDepth;\n        }\n        dfs(root->left, currDepth + 1, maxDepth, ans);\n        dfs(root->right, currDepth + 1, maxDepth, ans);\n    }\n\n    int findBottomLeftValue(TreeNode* root) {\n        if(!root->left && !root->right) return root->val;\n        int ans = 0;\n        int currDepth = 1;\n        int maxDepth = 0;\n        dfs(root, currDepth, maxDepth, ans);\n        return ans;\n    }\n};\n```\n```Python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:\n        ans = [0]\n        maxDepth = [0]\n        currDepth = 1\n        def dfs(root, currDepth):\n            if not root:\n                return\n            if currDepth > maxDepth[0]:\n                ans[0] = root.val\n                maxDepth[0] = currDepth\n            leftLeft = dfs(root.left, currDepth + 1)\n            rightLeft = dfs(root.right, currDepth + 1)\n        dfs(root, currDepth)\n        return ans[0]\n```
['Tree', 'Depth-First Search', 'Recursion', 'Binary Tree', 'C++', 'Python3']