Skip to content

Commit

Permalink
Balanced BST
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Nov 3, 2017
1 parent 63af633 commit 5bb595b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 110_balanced_binary_tree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test balanced_bst.c
63 changes: 63 additions & 0 deletions 110_balanced_binary_tree/balanced_bst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

static int depth(struct TreeNode* root, bool *balance)
{
if (!(*balance) || root == NULL) {
return 0;
}

int l = depth(root->left, balance) + 1;
int r = depth(root->right, balance) + 1;
if (*balance) {
int diff = l > r ? l - r : r - l;
*balance = diff < 2;
}
return l > r ? l : r;
}

static bool isBalanced(struct TreeNode* root)
{
bool balance = true;
depth(root, &balance);
return balance;
}

int main(void)
{
struct TreeNode root, n10, n11, n20, n21, n23, n30, n37;
root.val = 1;
n10.val = 2;
n11.val = 3;
n20.val = 4;
n21.val = 5;
n23.val = 6;
n30.val = 7;
n37.val = 8;
root.left = &n10;
root.right = &n11;
n10.left = &n20;
n10.right = &n21;
n11.left = NULL;
n11.right = &n23;
n20.left = &n30;
n20.right = NULL;
n21.left = NULL;
n21.right = NULL;
n23.left = NULL;
n23.right = &n37;
n30.left = NULL;
n30.right = NULL;
n37.left = NULL;
n37.right = NULL;

printf("%s\n", isBalanced(&root) ? "true" : "false");
return 0;
}

0 comments on commit 5bb595b

Please sign in to comment.