Я создал рекурсивную функцию для вычисления максимального пути двоичного дерева. Я получил обратную связь, что он не работает, но, по моему тесту, он дает правильный результат. Может кто-то мне помочь, пожалуйста?Найти максимальную сумму листа в корневой путь в двоичном дереве
private long PathSum(int row, int column, Pyramid pyramid)
{
// Base case, stop recurse when first row is reached.
if (row == 0) return pyramid[row, column];
// Set level to the current cell and add to the end result.
long value = pyramid[row, column];
// Continue to the next row.
if (row != 0)
{
// Continue to the next left level.
long left = pyramid[row - 1, column];
// Continue to the next right level.
long right = pyramid[row - 1, column + 1];
// Get the highest of the left and right.
long highest = Math.Max(left, right);
// Get the index of the hightest path.
int nextColumn = highest == left ? column : column + 1;
// Recurse to the next level and add results together.
value += GetTotal(row – 1, nextColumn, pyramid);
}
// Return result to the caller.
return value;
}
Задать вопрос, что они ожидали, что у них есть, и попытаться отладить его. Также это выглядит подозрительно для c 'Math.Max (слева, справа)'. – luk32
Это не похоже на C-код - больше похож на Java или C#. Что это за язык? Что такое «пирамида»? – dasblinkenlight
Я спросил, но они не могут сказать мне больше. Как я уже сказал, я протестировал его, и для меня все хорошо. – Doro