I have to write a program that can verify if the binary tree satisfies this property:
for each node of the tree, the height of its subtrees (right and left) must differ for max 1.
Not good:
Good:
The algorithm I built has a O((n^2)log(n))
complexity.
Thanks for Helping!
bool check(node* root){
if(!root->right && !root->left){
return;
}
int h;
int n_node;
int hRight, hLeft;
h = height(root);
hRight = height_right(root);
hLeft = height_left(root);
n_node = pow(2, heihgt+1)-1;
if((hRight > n_node/2 && hLeft <= n_node/4) || (hLeft > n_node/2 && hRight <= n_node/4)
return true;
}
I know is not good-looking but it is an attempt. Notice that this algorithm is O(nlog(n)) since it works only for the firts node (root). So i have to put it inside another block to scroll nodes.