DATA STRUCTURES PROGRAM • TREES & BINARY SEARCH TREES
Check Whether a Binary Tree Is Height-Balanced
Learn how to check whether a binary tree is height-balanced using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
Tree is balanced.
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
#include <stdlib.h>
struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; };
struct TreeNode *new_node(int value)
{
struct TreeNode *node = malloc(sizeof *node);
if (node == NULL) exit(EXIT_FAILURE);
node->data = value; node->left = NULL; node->right = NULL;
return node;
}
struct TreeNode *sample_tree(void)
{
struct TreeNode *root = new_node(1);
root->left = new_node(2); root->right = new_node(3);
root->left->left = new_node(4); root->left->right = new_node(5);
root->right->left = new_node(6); root->right->right = new_node(7);
return root;
}
void free_tree(struct TreeNode *root)
{
if (root == NULL) return;
free_tree(root->left); free_tree(root->right); free(root);
}
int balance_height(struct TreeNode *root)
{
if (root == NULL) return 0;
int left = balance_height(root->left);
int right = balance_height(root->right);
if (left < 0 || right < 0 || abs(left - right) > 1) return -1;
return 1 + (left > right ? left : right);
}
int main(void)
{
struct TreeNode *root = sample_tree();
puts(balance_height(root) >= 0 ? "Tree is balanced." : "Tree is not balanced.");
free_tree(root);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Tree is balanced.
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Return a failure marker as soon as any subtree heights differ by more than one.
- Display the computed result.
Return a failure marker as soon as any subtree heights differ by more than one.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(h)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
