Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions java/0110-balanced-binary-tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,32 @@ public static boolean isBalanced(TreeNode root) {
return dfs(root).getKey();
}
}

// Solution using the bottom up approach
// TC and SC is On

class Solution {

public int height(TreeNode root){
if(root == null){
return 0;
}

int lh = height(root.left);
int rh = height(root.right);

return 1 + Math.max(lh,rh);
}

public boolean isBalanced(TreeNode root) {

if(root == null){
return true;
}

int lh = height(root.left);
int rh = height(root.right);

return Math.abs(lh - rh) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
}