Java simple 2ms solution


  • 0
    V
    public class Solution {
        boolean answer = true;
        public boolean isBalanced(TreeNode root) {
            checkHeigth(root, 0);
            return answer;
        }
        
        private int checkHeigth(TreeNode node, int h){
            if(node == null) return h;
            int left = 0, right = 0;
            if(answer){
                left = checkHeigth(node.left, h+1);
                right = checkHeigth(node.right, h+1);
                if(Math.abs(left - right) > 1) answer = false;
            }
            return Math.max(left, right);
        }
    }

Log in to reply
 

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