Try the below one
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))
سؤال
i have this class in java for Nodes of a tree
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;
}
height of this node is (this.right.height - this.left.height)
and height of a node which has just 1 child and it's child is leaf is 1 if child is right child and -1 if child is left child for the node
how can i do this??
(i want to write an avl tree)
المحلول
Try the below one
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))