Friday 26 February 2016

Value Balanced Tree

One of the most important question mostly asked in placement .


A tree is called value balanced tree if for all nodes, sum of values (assume the values are integers) of nodes in left hand side is equal to sum of values in right hand side.

Given a complete binary tree find if it is a value balanced tree or not.

You have to complete isValueBalanced(int [], int) function. You can create other functions but should be called from given function.
Input Constraints:
The input tree would be always a complete binary tree.

Input:
Number of Nodes in first line and values of nodes in level order (as represented as array) in next.
Output:
Either “Tree is value balanced” or “Tree is not value balanced”.

Solution -:
After deadline is over

3 comments:

  1. bool isValueBalanced(int tree[],int count){
    int loop,a,b;

    loop=((count-1)/2) - 1;

    while(loop != -1){
    a=(2*loop) + 2;
    b=2*loop+1;

    if(tree[a] != tree[b])
    { return false;}
    else{
    tree[loop]=tree[loop] + tree[a]+tree[a];
    loop--;
    }

    }
    return true;

    }

    ReplyDelete