/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型
*/
#include <stdio.h>
void getMaxResult(struct TreeNode* root, int sum, int* max) {
if (!root) return;
if (root->left == NULL && root->right == NULL) {
sum += root->val;
(*max) = fmax((*max), sum);
}
getMaxResult(root->left, sum + root->val, max);
getMaxResult(root->right, sum + root->val, max);
}
int dfs(struct TreeNode* root) {
if (!root) return 0;
int left = 0, right = 0;
getMaxResult(root->left, 0, &left);
getMaxResult(root->right, 0, &right);
return root->val + left + right;
}
void get(struct TreeNode* root, int* max) {
if (!root) return;
int num = dfs(root);
(*max) = fmax((*max), num);
get(root->left, max);
get(root->right, max);
}
int maxMilkSum(struct TreeNode* root ) {
int max = -1;
get(root, &max);
return max;
}