Balanced Tree
Lesson 11 of 13 in Coddy's Binary Tree - Data Structures Series #3 course.
The next challenges use the BinaryTree you built. Use the provided implementation to solve them.
Challenge
EasyWrite a function named isBalanced that gets a string representing a BinaryTree (recursive [value, leftSubtree, rightSubtree] form, with null for empty) and returns true if the tree is balanced, otherwise false. A tree is balanced if for every node, the heights of its two subtrees differ by at most 1.
Try it yourself
#include <stdio.h>
#include <string.h>
#include "solution.h"
int main() {
char s[4096];
if (!fgets(s, sizeof(s), stdin)) s[0] = '\0';
s[strcspn(s, "\r\n")] = '\0';
printf("%s\n", isBalanced(s) ? "true" : "false");
return 0;
}