Skip to content

Create Balance binary tree #6040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.coding.patterns.tree;

class BalancedBinaryTree {

private static Pair<Boolean, Integer> dfs(TreeNode root) {
if (root == null) {
return new Pair<Boolean, Integer>(true, 0);
}

var left = dfs(root.left);
var right = dfs(root.right);

var balanced =
left.getKey() &&
right.getKey() &&
(Math.abs(left.getValue() - right.getValue()) <= 1);

return new Pair<Boolean, Integer>(
balanced,
1 + Math.max(left.getValue(), right.getValue())
);
}

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);
}
}
Loading