DimitarD

SAA kurs1 2.5.15

Apr 22nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. struct Node
  8. {
  9. int data;
  10. struct Node* left, *right;
  11. };
  12.  
  13. unsigned int gethalfCount(struct Node* root)
  14. {
  15. if (root == NULL)
  16. return 0;
  17.  
  18. int res = 0;
  19. if ((root->left == NULL && root->right != NULL) ||
  20. (root->left != NULL && root->right == NULL))
  21. res++;
  22.  
  23. res += (gethalfCount(root->left) + gethalfCount(root->right));
  24. return res;
  25. }
  26.  
  27. struct Node* newNode(int data)
  28. {
  29. struct Node* node = new Node;
  30. node->data = data;
  31. node->left = node->right = NULL;
  32. return (node);
  33. }
  34.  
  35.  
  36. int main(void)
  37. {
  38. /* 2
  39. / \
  40. 7 5
  41. \ \
  42. 6 9
  43. / \ /
  44. 1 11 4
  45.  
  46.  
  47. struct Node *root = newNode(2);
  48. root->left = newNode(7);
  49. root->right = newNode(5);
  50. root->left->right = newNode(6);
  51. root->left->right->left = newNode(1);
  52. root->left->right->right = newNode(11);
  53. root->right->right = newNode(9);
  54. root->right->right->left = newNode(4);
  55.  
  56. cout << gethalfCount(root);
  57.  
  58. return 0;
  59. }
Add Comment
Please, Sign In to add comment