当前位置: 代码迷 >> 综合 >> Leetcode 1302. 层数最深叶子节点的和(DAY 7)
  详细解决方案

Leetcode 1302. 层数最深叶子节点的和(DAY 7)

热度:93   发布时间:2023-11-17 20:40:15.0

原题题目

在这里插入图片描述




代码实现(首刷自解)

/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/int maxlevel;
int sum;void depthfirstsearch(struct TreeNode* root,int level)
{
    if(root){
    if(level + 1 > maxlevel){
    sum = root->val;maxlevel = level + 1;}else if(level + 1 == maxlevel)sum += root->val;depthfirstsearch(root->left,level+1);depthfirstsearch(root->right,level+1);}
}int deepestLeavesSum(struct TreeNode* root){
    maxlevel = 0,sum = 0;depthfirstsearch(root,0);return sum;
}