Bloomberg LP Interview Question
Financial Application EngineersCountry: United States
Interview Type: In-Person
Do we have to calculate the sum of values from root to each leaf and then identify the path with maximum value? If so, Googler's solution seems to be in line, but seems to be working differently by the solution from yogi.rulzz. Or am I missing something here :(
void sumValue(Node *n, list<int> iValueList, int value)
- Googler October 26, 2013{
if( n == 0 )
{
return;
}
value += n->value;
if( n->left == 0 && n->right == 0 )
{
printf("leaf node v:%d\n", value);
iValueList.push_front(value);
}
sumValue(n->left, iValueList, value);
sumValue(n->right, iValueList, value);
}
bool findLeafMaxValueSum(int& out)
{
list<int> list;
int value = 0;
sumValue( root, list ,value);
out = 0;
if( list.size() > 0 )
{
list<int>::iterator itr;
for( itr = list.began(); itr != list.end(); itr++ )
{
if( *itr > out )
{
out = *itr;
}
}
return true;
}else{
return false;
}
}