Skip to content

Commit ba38a33

Browse files
authored
Merge pull request #3181 from snehit221/0110-balanced-binary-tree-java-simple-solution
added 0110 balanced binary tree simplified java solution
2 parents b98a126 + a253845 commit ba38a33

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

java/0110-balanced-binary-tree.java

+29
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ public static boolean isBalanced(TreeNode root) {
2525
return dfs(root).getKey();
2626
}
2727
}
28+
29+
// Solution using the bottom up approach
30+
// TC and SC is On
31+
32+
class Solution {
33+
34+
public int height(TreeNode root){
35+
if(root == null){
36+
return 0;
37+
}
38+
39+
int lh = height(root.left);
40+
int rh = height(root.right);
41+
42+
return 1 + Math.max(lh,rh);
43+
}
44+
45+
public boolean isBalanced(TreeNode root) {
46+
47+
if(root == null){
48+
return true;
49+
}
50+
51+
int lh = height(root.left);
52+
int rh = height(root.right);
53+
54+
return Math.abs(lh - rh) <= 1 && isBalanced(root.left) && isBalanced(root.right);
55+
}
56+
}

0 commit comments

Comments
 (0)