Advertisement
minh_tran_782

distinctParities

Apr 26th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. int sumTree (BTNode* root)
  2. {
  3.     if (root == NULL) return 0;
  4.     return root->val + sumTree(root->left) + sumTree(root->right);
  5. }
  6.  int distinctParities(BTNode* root) {
  7.      if (root == NULL) return 0;
  8.       if (root->left == NULL) return distinctParities(root->right);
  9.      if (root->right == NULL) return distinctParities(root->left);
  10.       int sumLeft = sumTree(root->left);
  11.     int sumRight = sumTree(root->right);
  12.      if ((sumLeft % 2) + (sumRight % 2) % 2 == 1) return 1 + distinctParities(root->left) + distinctParities(root->right) ;
  13.      return distinctParities(root->left) + distinctParities(root->right);
  14.  
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement