Java judges whether a binary tree is a balanced binary tree

Determine whether a binary tree is a balanced binary tree

topic description

A height-balanced binary tree is defined as: The absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.

example

insert image description here
insert image description here

insert image description here

Original title OJ link

https://leetcode.cn/problems/balanced-binary-tree/

answer code

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
 import java.lang.Math;
class Solution {
    
    
    public boolean isBalanced(TreeNode root) {
    
    
        if(root == null){
    
    
            return true;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        if((Math.abs(leftH-rightH)<=1) && isBalanced(root.left) && isBalanced(root.right)){
    
    
            return true;
        }
        return false;


    }
    public int getHeight(TreeNode root){
    
    //求树的深度(高度)
        if(root == null){
    
    
            return 0;
        }
        if(root.right == null && root.left == null){
    
    
            return 1;
        }
//        int count1 = getHeight(root.left)+1;
//        int count2 = getHeight(root.right)+1;
//        return Math.max(count1,count2);
        return Math.max(getHeight(root.left)+1, getHeight(root.right)+1);
    }
}

One disadvantage of this code is that it actually does a lot of repetitive work. In fact, it is performing double-layer recursion. Each node calculates the height of its left and right subtrees. Only when the absolute value of the dissatisfied difference is less than or equal to 1 will it return false , but in fact, this work can be done directly when calculating the height. If the absolute value of the difference between the left and right subtree heights is greater than 1, it will directly return -1. The height of the subsequent nodes does not need to be calculated at all, because this shows that the tree must be It is unbalanced.
So there is the following code

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
 import java.lang.Math;
class Solution {
    
    
    public boolean isBalanced(TreeNode root) {
    
    
        return maxDepth(root)>=0;
    }
    public int maxDepth(TreeNode root){
    
    
        if(root == null){
    
    
            return 0;
        }
       int count1 = maxDepth(root.left);
       if(count1<0){
    
    
           return-1;
       } 
       int count2 = maxDepth(root.right);
       if(count2<0){
    
    
           return -1;
       }
       if(Math.abs(count1-count2)<=1){
    
    
           return Math.max(count1,count2)+1;
       }
       else{
    
    
           return -1;
       }
    }
}

Guess you like

Origin blog.csdn.net/baixian110/article/details/130943838