Binary tree - determine whether a tree is a balanced binary tree

Balanced binary tree (empty tree or the height difference between the left and right children does not exceed 1)

Recursive functions are very useful when it comes to binary tree problems

List the possibilities - "sort out the type of return value -" the entire recursive process obtains the information of the subtree according to the same structure, integrates the information of the subtree, processes the information that should be returned, and returns upwards

1. Is the left subtree balanced?

2. Is the right subtree balanced?

3. The height of the left subtree

4. The height of the right subtree

 

Depending on the possibility, use a recursive function

Possibility - "Type of return value  

/ / List the possibility of whether the left and right subtrees are balanced respectively, if balanced, the height of the left and right subtrees
    public static class ReturnData{
        boolean isB;
        int high;

        public ReturnData(boolean isB, int high){
            this.isB = isB;
            this.high = high;
        }
    }

  

The entire recursive process obtains the information of the subtree according to the same structure ( whether the left subtree and the right subtree are balanced, and their heights ), integrates the information of the subtree ( whether the height difference between the left and right subtrees meets the requirements ), and processes the return. information (should return the left and right subtrees, the one with the larger height is high+1 )

public static ReturnData process(Tree tree){
        if(tree == null) return new ReturnData(true, 0);


        ReturnData leftData = process(tree.left);
        if(!leftData.isB){
            return new ReturnData(false, 0);
        }

        ReturnData rightData = process(tree.right);
        if(!rightData.isB){
            return new ReturnData(false, 0);
        }

        if(Math.abs(leftData.high - rightData.high) > 1){
            return new ReturnData(false, 0);
        }

        return new ReturnData(true, Math.max(leftData.high, rightData.high) + 1);
    }

  

 

overall code

public class IsBanlancedTree {

    / / List the possibility of whether the left and right subtrees are balanced respectively, if balanced, the height of the left and right subtrees
    public static class ReturnData{
        boolean isB;
        int high;

        public ReturnData(boolean isB, int high){
            this.isB = isB;
            this.high = high;
        }
    }


    public static ReturnData process(Tree tree){
        if(tree == null) return new ReturnData(true, 0);


        ReturnData leftData = process(tree.left);
        if(!leftData.isB){
            return new ReturnData(false, 0);
        }

        ReturnData rightData = process(tree.right);
        if(!rightData.isB){
            return new ReturnData(false, 0);
        }

        if(Math.abs(leftData.high - rightData.high) > 1){
            return new ReturnData(false, 0);
        }

        return new ReturnData(true, Math.max(leftData.high, rightData.high) + 1);
    }

    public static boolean isBanlancedTree(Tree tree){
        return process(tree).isB;
    }
}

  

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850201&siteId=291194637