Given a binary tree, determine whether it is a highly balanced binary tree. In this case, a highly 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 is no more than 1.Copy the code
class Solution { public boolean isBalanced(TreeNode root) { if(root == null){ return true; } int leftLength = getDeep(root.left); int rightLength = getDeep(root.right); int a = Math.abs(leftLength-rightLength); Return isBalanced(root.left) &&isbalanced (root.right); if(a<=1){return isBalanced(root.left) &&isbalanced (root.right); } return false; } public int getDeep(TreeNode root){if(root == null){return 0; } int leftDept = getDeep(root.left); int rightDept = getDeep(root.right); return Math.max(leftDept,rightDept)+1; }}Copy the code