My java solution

  • 0
    public class Solution {
        boolean isBalanced=true;
        public boolean isBalanced(TreeNode root) {
            if(root==null) return true;
            if(!isBalanced) return isBalanced;
            int LeftHeight=countHeignt(root.left);
            int RightHeight=countHeignt(root.right);
            isBalanced= Math.abs(LeftHeight-RightHeight)>1?false:true;
            return isBalanced(root.left) && isBalanced(root.right);
        public int countHeignt(TreeNode root){
            if(root==null) return 0;
            return Math.max(countHeignt(root.left)+1,countHeignt(root.right)+1);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.