Я не попал в точку, где ans = 15 и sum = 15. Теперь у 15 нет дочерних элементов, поэтому он не будет go в for для l oop. Тогда он напрямую вернет ans (15) к 1? Что будет дальше?
Или, пожалуйста, объясните мне всю dry часть прогона. Ввод: 5 3 1 2 3 1 15 2 4 5 1 6 0 0 0 0
TreeNode<int>* maxSumNode(TreeNode<int> *root){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
//small calculation
TreeNode<int>* ans = root;
int sum = root -> data;
for(int i = 0; i < root -> children.size(); i++)
{
sum = sum + root -> children[i] -> data;
}
for(int i = 0; i < root -> children.size(); i++)
{
TreeNode<int> *x = maxSumNode(root -> children[i]);
int xSum = x -> data;
for(int i = 0; i < x ->children.size(); i++)
{
xSum = xSum + x ->children[i] -> data;
}
if(xSum > sum)
{
ans = x;
sum = xSum;
}
}
return ans;
}