繁体   English   中英

递归查找二叉树中节点的高度之和

[英]Find sum of nodes height in a binary tree recursively

如何在二叉树中递归地找到节点的高度之和?

例:

在此处输入图片说明

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。 不是很好。

一个简单的版本是执行两次通过过程,在该过程中,您首先为每个节点记录其所处的高度,然后遍历节点以将其汇总。 可以使该方法递归,但是在计算高度时只需累加一次就很容易做到。

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

递归地找到每个节点的高度,并继续添加到静态变量中。 或者,您可以记住高度并将其存储在每个节点中,然后进行另一次递归以将它们相加。

递归应返回节点n的高度,而不是子树中每个节点的总高度。

给定您实现的节点高度,我们简单地将其称为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);
}

看看是否有一种算法可以一次递归地进行计算(也许有些回溯算法?)会很有趣。

好。 我已经解决了。

a)如果n == null返回0

b)如果n.left == null && n.right == null返回0

c)总高度是total height of left total height of right + 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;
}

我假设您在插入过程中不更新高度。

解决方案:我将按顺序遍历树。 所以我首先声明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();
}

因此,您现在将拥有可以存储所有高度的水平仪。

替代方法可以是使用队列的广度优先搜索遍历,但答案将是相同的。 希望这可以帮助。

void addHeights(class tree * root,int depth,int&sum){if(root){addHeights(root-> left,depth + 1,sum);
addHeights(root-> right,depth + 1,sum); 总和=深度 }}

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

}

尽管我假设您将节点内部的数据称为“数据”,但我仍需要更多数据来评估您的代码。

因此,如果节点为null,则意味着您已到达树的末端并返回0。否则,它将获取数据并从左向右遍历。 每次递归时,它们都会被添加,直到不再有要添加的节点为止。

 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。

计数功能位于最后一条语句中。 那就是获得最大的身高。

主要课程是在使用该方法时熟悉递归和编程堆栈。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM