/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int res = 0;
int maxDepth = 0;
void traversal(TreeNode* root, int depth) {
if (!root) return;
if (depth > maxDepth) {
maxDepth = depth;
res = root->val;
}
traversal(root->left, depth + 1);
traversal(root->right, depth + 1);
}
public:
int findBottomLeftValue(TreeNode* root) {
traversal(root, 1);
return res;
}
};