如何递归地在二叉树中找到节点高度的总和?
示例:
public int totalHeight() {
return totalHeight(root);
}
private int totalHeight(BinaryNode<AnyType> n) {
int totalHeight = 0;
if (n.left == null && n.right == null)
return totalHeight;
if (n.left != null && n.right != null)
return totalHeight + 1
+ Math.max(totalHeight(n.left), totalHeight(n.right));
if (n.left != null)
return totalHeight + 1 + Math.max(totalHeight(n.left), -1);
if (n.right != null)
return totalHeight + 1 + Math.max(-1, totalHeight(n.right));
return totalHeight;
}
我试过这个,但它只获得树的高度而不是所有节点高度的总和。
我觉得很难在递归中跟踪计数器,似乎totalHeight
每次递归调用都设置为0。这不好。
答案 0 :(得分:1)
一个简单的版本是做一个两遍过程,你首先为每个节点记录它所处的高度,然后迭代通过节点来总结它们。这种方法可以递归,但在计算高度时通过求和只需一次就可以轻松完成。
public static int totalHeightSum = 0;
private int calculateHeightAndAdd ( Node n )
{
if ( n == null )
return 0;
int leftHeight = calculateHeightAndAdd ( n.left );
int rightHeight= calculateHeightAndAdd ( n.right);
int myHeight = 1 + Math.max ( leftHeight, rightHeight );
totalHeightSum += myHeight;
return myHeight;
}
答案 1 :(得分:0)
反复查找每个节点的高度并继续添加到静态变量。或者,您可以记住每个节点中的高度和存储,然后执行另一次递归来添加它们。
递归应该返回节点n的高度,而不是子树中每个节点的总高度。
答案 2 :(得分:0)
鉴于您对节点高度的实现,我们只需将其称为height(BinaryNode<?>)
,您可以:
如果您有权访问集合中的所有节点:
int sumHeight(List<BinaryNode<?>> nodes) {
int sum = 0;
for (BinaryNode<?> node: nodes) {
sum += height(node);
}
return sum;
}
如果您只能访问树结构中的节点:
int sumHeight(BinaryNode<?> node) {
if (node == null) return 0;
return height(node) + sumHeight(node.left) + sumHeight(node.right);
}
看看是否有算法可以在一次递归中进行计算(可能是一些回溯算法?)会很有趣。
答案 3 :(得分:0)
确定。我已经找到了解决方案。
a)如果n == null
返回0
b)如果n.left == null && n.right == null
返回0
c)总高度为total height of left
+ total height of right
+ the height of it self
1)如果左侧较大,则左侧的总高度减去左侧的总高度
2)如果右侧较大,则右侧总高度减去右侧右侧总高度
public int totalHeight() {
return totalHeight(root);
}
private int totalHeight(BinaryNode<AnyType> n) {
if (n == null)
return 0;
else if (n.left == null && n.right == null)
return 0;
else
return totalHeight(n.left)
+ totalHeight(n.right)
+ (totalHeight(n.left) > totalHeight(n.right) ? totalHeight(n.left)
- (n.left == null ? 0 : totalHeight(n.left.left))
: totalHeight(n.right)
- (n.right == null ? 0
: totalHeight(n.right.right))) + 1;
}
答案 4 :(得分:0)
我假设您在插入过程中没有更新高度。
解决方案: 我会以一种有序的方式穿过树。所以我首先声明root.height = 0。
然后说
BinaryNode right;
BinaryNode left;
BinaryNode parent;
static int level;
int height;
if(left!=null)
{
left.height=left.parent.height+1;
level=level+left.height;
left.yourMethod();
}
if(right!=null)
{
right.height= right.parent.height+1;
level=level+right.height;
right.yourMethod();
}
所以你现在将拥有存储所有高度的等级。
替代方法可以是使用队列的广度优先搜索遍历,但答案是相同的。 希望这会有所帮助。
答案 5 :(得分:0)
void addHeights(class tree * root,int depth,int&amp; sum)
{
如果(根)
{
addHeights(root-&gt; left,depth + 1,sum);
addHeights(root-&gt; right,depth + 1,sum);
总和+ =深度;
}
}
答案 6 :(得分:0)
public int sum(){
return sum(root);
}
private int sum(BinaryNode <?> n){
if(n == null)
return 0;
else
return n.data + sum(n.left) + sum(n.right);
}
我需要更多数据来评估你的代码,虽然我假设你在节点“data”中调用了数据。
因此,如果节点为空,则表示您已到达树的末尾并返回0.否则,它将获取数据并向左移动然后向右移动。随着每次递归,它们将被添加,直到它们不再有剩余的节点被添加。
答案 7 :(得分:-1)
private int maxHeight(BinaryNode<AnyType> n) {
if (n ! = null) return 0;
int leftheight = maxHeight(n.left);
int rightheight = maxHeight(n.right);
return (leftheight > rightheight) ? leftheight + 1 : rightheight + 1;
}
到目前为止,您已知道计算高度的4个案例
如果存在左孩子或右孩子,本质是继续左或右节点。 如果存在,返回1.
计数功能在最后一个语句中。这是为了获得最大的高度。
主要课程是在方法工作时熟悉递归和编程堆栈。