【问题标题】:how to find the height of right child minus height of the left child in a tree如何在树中找到右孩子的高度减去左孩子的高度
【发布时间】:2014-01-17 10:05:07
【问题描述】:

我在 java 中有这个类用于树的节点

public class Node {

Node (int v, Node lt, Node rt){
    value = v;
    left = lt;
    right = rt;
    height = 0;
    parent = null;
}

Node (int v){
    this (v, null, null);
}

int value;
int height;
Node left;
Node right;
Node parent;
}

这个节点的高度是(this.right.height - this.left.height)

如果子节点是右子节点,则只有 1 个子节点且其子节点为叶子节点的高度为 1,如果子节点为该节点的左子节点,则该节点的高度为 -1

我该怎么做??

(我想写一棵avl树)

【问题讨论】:

标签: java data-structures binary-tree avl-tree


【解决方案1】:

试试下面这个

public int height(Node root){
       if(root == null)return 0;
       return 1+Max(height(root.left),height(root.right));
    }

    heightDifference = (height(this.right) - height(this.left)) 

【讨论】:

    猜你喜欢
    • 2018-04-28
    • 2013-02-13
    • 2015-05-31
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2016-10-22
    相关资源
    最近更新 更多