Answer to Question #200995 in Java | JSP | JSF for Zahra

Question #200995

Design a method that determines whether or not a binary tree is fully balanced.

This method takes no parameters, and returns a boolean value: true if the tree is

fully balanced, and false if it is not.


1
Expert's answer
2021-05-31T00:54:14-0400
public boolean isBalanced() {
    return isBalanced(root);
}

private boolean isBalanced(Node node) {
    if (node == null)
        return true;
    int leftHeight = height(node.left);
    int rightHeight = height(node.right);
    return Math.abs(leftHeight - rightHeight) <= 1 && isBalanced(node.left) && isBalanced(node.right);
}

private int height(Node node) {
    return node == null ? 0 : 1 + Math.max(height(node.left), height(node.right));
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS