我創建了一個遞歸函數來計算二叉樹的最大路徑。我得到的反饋是它不起作用,但根據我的測試,它提供了正確的結果。有人能幫助我嗎?在二叉樹中查找最大總和葉根路徑
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