fuxiang90
BAN USERhow about my code with c++ ,and i think i can print well the path .
I use a vector to store the path
int max_sum = 0;
vector< vector<int> > max_sum_path;
void getMaxSum(struct node *head , int curr_sum ,vector<int> &path)
{
curr_sum += head->x;
if(curr_sum > max_sum){
max_sum = curr_sum;
max_sum_path.erase(max_sum_path.begin() , max_sum_path.end());
}
if(head->l ){
path.push_back(1);// 1 :left 2 :rigth
getMaxSum(head->l , curr_sum , path);
path.pop_back();
}
if(head->r ){
path.push_back(2);// 1 :left 2 :rigth
getMaxSum(head->r , curr_sum , path);
path.pop_back();
}
if(curr_sum == max_sum){
max_sum_path.push_back(path);
}
}
If the space is unlimit ,this answer is right
- fuxiang90 January 19, 2013