gpt4 book ai didi

c# - 在二叉树中找到叶到根路径的最大总和

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:44 26 4
gpt4 key购买 nike

<分区>

我创建了一个递归函数来计算二叉树的最大路径。我得到的反馈是它不起作用,但根据我的测试,它提供了正确的结果。有人能帮助我吗?

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;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com