Re: [閒聊] 每日leetcode

作者: DJYOSHITAKA (Evans)   2024-02-28 15:37:00
大家好像差不多
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;
}
};
作者: NCKUEECS (小惠我婆)   2024-02-28 15:54:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com