patternjavaMinor
Finding if the tree is balanced or not
Viewed 0 times
thebalancedfindingnottree
Problem
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 && root.left == null && 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) <= 1 ? true : false;
}
}
//find the height of the tree
public int findHeight(TreeNode root) {
if (root == null) {
return 0;
} else if (root.left == null && root.right == null) {
return 1;
} else if (root.left == null && root.right != null) {
return 1 + findHeight(root.right);
} else if (root.left != null && root.right == null) {
return 1 + findHeight(root.left);
} else {
return Math.max(1+findHeight(root.left), 1+findHeight(root.right));
}
}
}Solution
Definition of balanced
The usual definition of a balanced binary tree:
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:
You can drop the
This is exactly the same:
This chain of conditions has the same issue,
with some unnecessary conditions that can be eliminated:
Can be simplified to:
Using boolean expressions
You can use boolean expressions directly. For example like this:
Other simplifications
Instead of this:
Since
you could simplify the above as:
Another thing, instead of adding a constant in both sides of a
You could do just once:
On closer look, you could simplify the entire
Naming
Instead
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 && root.left == null && 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 && 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 && root.right == null) {
return 1;
} else if (root.left == null && root.right != null) {
return 1 + findHeight(root.right);
} else if (root.left != null && root.right == null) {
return 1 + findHeight(root.left);
} else {Can be simplified to:
if (root == null) {
return 0;
} else if (root.left == null && 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) <= 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.Code Snippets
if (root == null || (root != null && root.left == null && root.right == null)) {if (root == null || (root.left == null && root.right == null)) {if (root == null) {
return 0;
} else if (root.left == null && root.right == null) {
return 1;
} else if (root.left == null && root.right != null) {
return 1 + findHeight(root.right);
} else if (root.left != null && root.right == null) {
return 1 + findHeight(root.left);
} else {if (root == null) {
return 0;
} else if (root.left == null && 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 {return Math.abs(balancedLeft - balancedRight) <= 1;Context
StackExchange Code Review Q#119934, answer score: 3
Revisions (0)
No revisions yet.