maximum sum of value from root to leaf in a binary tree using stack -


i trying find maximum sum of value root leaf nodes in binary tree using stack. wrote following code there bug in . <>

stacks s; s.push(root); maxsum=currsum=0;       while(!s.isempty()) {         temp = s.top();         s.pop();         if( temp->left == null && temp->right == null ) {           currsum = currsum+temp->data;           if(currsum > maxsum) {             maxsum = currsum;           }           currsum =0;         } else {           currsum = currsum + temp->data;           if(temp->left) s.push(temp->left);          if(temp->right) s.push(temp->right);          }          } 

what trying calculate sum till leaf node , assign maxsum.
ex:- binary tree

       1       /   \     2      3   /  \ 4     5 

1)i first push 1 , pop . currsum =1;
2) push 3 , 2 , pop 2. cursum = 3 , push 5 , 4;
3) stack looks 4<-5-<3-<1 (4 top element)
4)now 4 leaf node , enter if loop , add currsum = 3+4=7 , pop 4 .
5)now temp 5 , set currsum=0, currsum when pop 5 becomes 5 .

can me fix bug please

    10   /   \  8     2 

consider example code.

  1. first root(10) pushed stack , popping it.
  2. now currsum becomes 10 , left (8) , right (2) nodes pushed stack.
  3. now topmost element(2) popped , added currsum.now currsum becomes 12.
  4. as reached leaf node, maxsum contains currsum value.
  5. and currsum made 0.

this mistake losing root value.

  1. now pop last element (8) , currsum has value 8.as reached leaf node,we compare maxsum(12) , final answer 12 wrong.

the code below through. try using stack in place of vector.

using namespace std;

struct node {  int data;  struct node* left;  struct node* right;  };   struct node* createnode(int k) {  struct node* temp = new node;  temp->left = null;  temp->right = null;  temp->data = k;   return temp;   }   int tsum(vector<struct node *> path) {   int sum;   int n = path.size();   int i;   (i = 0; < n; i++)   sum = sum + path[i]->data;   return sum;   }      int maxsum(struct node *root) {      int currsum = 0, maxsum = 0;     vector<struct node *> v;         v.push_back(root); //push root     vector<int> visited(100, 0);      while (v.size() > 0) {     visited[v.back()->data] = 1; //whenever node reached mark visited      if (v.back()->left != null && !visited[v.back()->left->data])         v.push_back(v.back()->left);     else if (v.back()->right != null && !visited[v.back()->right->data])         v.push_back(v.back()->right);     else {         if (!v.back()->left && !v.back()->right) {             currsum = tsum(v);             if (currsum > maxsum)                 maxsum = currsum;          }         v.pop_back(); //pop here used backtracking       }        }      return maxsum;     }   int main() {  int sum = 0; std::vector<int> arr; /* constructed binary node        1      /   \     2      3   /  \      4     5    */ struct node *root = createnode(1); root->left = createnode(2); root->right = createnode(3); root->left->left = createnode(4); root->left->right = createnode(5);  int s = 0;  s = maxsum(root); cout << "sum" << s << "\n";  return 0;  } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -