CreepyMan
BAN USER- 0of 0 votes
AnswersGiven a binary tree, every node has a int value, return the root node of subtree with the largest sum up value. Java is more preferable. Caution: the return should be a node, not a integer!
- CreepyMan in United States| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer Trees and Graphs
void printtree(Node* p,int level) {
vector<Node*> av;
av.push_back(p);
map<int,vector<Node*> > amap;
amap[0]=av;
while(true) {
vector<Node*> nodev;
for(int i=0;i<amap[level].size();i++) {
Node* an = amap[level][i];
cout << an->num << endl;
if (an->left!=NULL) {
nodev.push_back(an->left);
}
if (an->right!=NULL) {
nodev.push_back(an->right);
}
}
if (nodev.size()>0) {
amap[++level]=nodev;
}
else
break;
}
}
void print(Node* p) {
printtree(p,0);
}
I got the same question.... lol
- CreepyMan March 06, 2012