JAVA 3ms easy and clear

  • 2
    public boolean isBalanced(TreeNode root) {
    		if(root==null) return true;
    		if(Math.abs(deepchar(root.left)-deepchar(root.right))>1) return false;
    		if(isBalanced(root.left)==false) return false;
    		if(isBalanced(root.right)==false) return false;
    		return true;
    	public int deepchar(TreeNode root){
    		int deep =0;
    		if(root==null) return 0;
    		else deep =1;
    		int dl =deepchar(root.left);
    		int dr =deepchar(root.right);
    		deep+= dl>dr? dl:dr;
    		return deep;

  • 0

    "isBalanced(root.left)==false" can be better validated as "!isBalanced(root.left) "

Log in to reply

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