Untitled

 avatar
unknown
plain_text
2 years ago
1.1 kB
4
Indexable
 public boolean isPerfect(){
        int treeHeight = getHeight(root);
        return isPerfect(root, 0, treeHeight);
    }

    private boolean isPerfect(Node node, int level, int treeHeight){
        if(node == null) return true;

        if(node.leftChild == null && node.rightChild == null){
            return level == treeHeight -1;
        }

        if(node.leftChild == null || node.rightChild == null) return false;

        return isPerfect(node.leftChild, level + 1, treeHeight) && isPerfect(node.rightChild, level + 1, treeHeight);
    }

    public boolean isFull(){
        return isFull(root);
    }

    private boolean isFull(Node node){
        if(node == null) return true;

        if((node.leftChild == null && node.rightChild != null) || (node.leftChild != null && node.rightChild == null)) return false;

        return isFull(node.leftChild) && isFull(node.rightChild);
    }


    private int getHeight(Node node){
        if(node == null) return 0;

        int leftHeight = getHeight(node.leftChild);
        int rightHeight = getHeight(node.rightChild);

        return Math.max(leftHeight, rightHeight) + 1;
    }
Editor is loading...