fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node
  6. {
  7. int data;
  8. struct node *left;
  9. struct node *right;
  10. };
  11.  
  12. struct node* add(int x)
  13. {
  14. struct node *temp = ( struct node *) malloc(sizeof(struct node));
  15. temp->data = x;
  16. temp->left = NULL;
  17. temp->right = NULL;
  18. return temp;
  19.  
  20. };
  21.  
  22.  
  23. void sum_tree( struct node *root)
  24. {
  25.  
  26. if ( root != NULL ) {
  27.  
  28. sum_tree(root->left);
  29. sum_tree(root->right);
  30.  
  31. int l = 0;
  32. int r = 0;
  33. if (root->left != NULL ){
  34. cout <<(root->left)->data<<endl;
  35. l = (root->left)->data;
  36. cout << l << endl;
  37. }
  38. if ( root->right != NULL ){
  39. cout << " anu" << endl;
  40. r = (root->right)->data;
  41. cout << r << endl;
  42. }
  43. //cout << root->data <<endl;
  44. // cout << l << r << endl;
  45. root->data = l+r;
  46. /*
  47.   if ( root->left != NULL ) {
  48.   if ( root->left->left == NULL && root->left->right == NULL ){
  49.   root->left->data = 0;
  50.   }
  51.   }
  52.   if ( root->right != NULL) {
  53.   if ( root->right->left == NULL && root->right->right == NULL ){
  54.   root->right->data = 0;
  55.   }
  56.   }
  57.   */
  58.  
  59. }
  60. return;
  61. }
  62.  
  63. int main()
  64. {
  65. struct node *root = NULL;
  66.  
  67. root = add(1);
  68. root->left = add(2);
  69. root->right = add(3);
  70. root->left->left = add(4);
  71. root->left->right = add(5);
  72. root->right->left = add(6);
  73. root->right->right = add(7);
  74.  
  75. sum_tree(root);
  76. return 0;
  77.  
  78. }
  79.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
0
0
 anu
0
0
0
 anu
0
0
0
 anu
0