大家好像差不多
DFS先走左邊 記下當層level跟目前ans的level
若level更深就更新
class Solution {
public:
void dfs(TreeNode* root, int level, int* ans_level, int* ans)
{
if(!root)
{
return;
}
if(level > *ans_level)
{
*ans = root->val;
*ans_level = level;
}
dfs(root->left, level+1, ans_level, ans);
dfs(root->right, level+1, ans_level, ans);
}
int findBottomLeftValue(TreeNode* root) {
int ans = root->val;
int ans_level = 0;
dfs(root, 0, &ans_level, &ans);
return ans;
}
};