Finding if the tree is balanced or not Can someone please review my code and let me know if there are some bugs or possible improvements?

    /**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    //return true if the root node is null or there is only root node.
    public boolean isBalanced(TreeNode root) {
        if (root == null || (root != null &amp;&amp; root.left == null &amp;&amp; root.right == null)) {
            return true;
        } else {
            int balancedRight = 0;
            int balancedLeft = 0;
            if (root.left != null) {
                balancedLeft = findHeight(root.left);
            }
            if (root.right != null) {
                balancedRight = findHeight(root.right);
            }

            return Math.abs(balancedLeft - balancedRight) &lt;= 1 ? true : false;
        }

    }
//find the height of the tree
    public int findHeight(TreeNode root) {
        if (root == null) {
            return 0;
        } else if (root.left == null &amp;&amp; root.right == null) {
            return 1;
        } else if (root.left == null &amp;&amp; root.right != null) {
            return 1 + findHeight(root.right);
        } else if (root.left != null &amp;&amp; root.right == null) {
            return 1 + findHeight(root.left);
        } else {
            return Math.max(1+findHeight(root.left), 1+findHeight(root.right));
        }
    }
}
Definition of balanced

The usual definition of a balanced binary tree:


The left and right subtrees' heights differ by at most one, AND
The left subtree is balanced, AND
The right subtree is balanced


Your algorithm only checks the level of the root, comparing the height of the left and right branches of the root. It doesn't go deeper, so the left or right subtrees may be unbalanced.

Unnecessary conditions

This condition can be simplified:


if (root == null || (root != null &amp;&amp; root.left == null &amp;&amp; root.right == null)) {



You can drop the root != null, because it will always be true.
This is exactly the same:

if (root == null || (root.left == null &amp;&amp; root.right == null)) {


This chain of conditions has the same issue,
with some unnecessary conditions that can be eliminated:


if (root == null) {
    return 0;
} else if (root.left == null &amp;&amp; root.right == null) {
    return 1;
} else if (root.left == null &amp;&amp; root.right != null) {
    return 1 + findHeight(root.right);
} else if (root.left != null &amp;&amp; root.right == null) {
    return 1 + findHeight(root.left);
} else {



Can be simplified to:

if (root == null) {
    return 0;
} else if (root.left == null &amp;&amp; root.right == null) {
    return 1;
} else if (root.left == null) {
    return 1 + findHeight(root.right);
} else if (root.right == null) {
    return 1 + findHeight(root.left);
} else {


Using boolean expressions

You can use boolean expressions directly. For example like this:

return Math.abs(balancedLeft - balancedRight) &lt;= 1;


Other simplifications

Instead of this:


int balancedRight = 0;
int balancedLeft = 0;
if (root.left != null) {
    balancedLeft = findHeight(root.left);
}
if (root.right != null) {
    balancedRight = findHeight(root.right);
}



Since findHeight immediately does a check for the case of root == null,
you could simplify the above as:

int balancedRight = findHeight(root.left);
int balancedLeft = findHeight(root.right);


Another thing, instead of adding a constant in both sides of a Math.max:


return Math.max(1+findHeight(root.left), 1+findHeight(root.right));



You could do just once:

return 1 + Math.max(findHeight(root.left), findHeight(root.right));


On closer look, you could simplify the entire findHeight method to this:

public int findHeight(TreeNode root) {
    if (root == null) {
        return 0;
    }
    return 1 + Math.max(findHeight(root.left), findHeight(root.right));
}


Naming

Instead balancedLeft and balancedRight,
leftHeight and rightHeight would have made a lot more sense.