this is the kind of an example Im talking about

Hey, this is a question from my handouts, but how would I go about writing a recursive program that would return the number of pairs of an ancestor and a child, whose sum equals to n, given that the program takes inputs of (tree t, sum a)

for example given this tree and 13, it will return 5 in the end, 13+0, 15-2, 6+7, 24-11, 12+1 (only ancestor and child, not siblings)

btw the tree itself is expressed in the following way: black indicates a child whereas red indicates a sibling.

enter image description here

  • 2
    $\begingroup$ Did you try to solve yourself? Are you familiar with recursion, preorder/inorder/postorder binary tree traversals? $\endgroup$ – fade2black Sep 25 '17 at 7:03
  • $\begingroup$ Yes, but all my solutions include bringing in additional variables, we can only use the two inputs as I understand ( treet and int a) $\endgroup$ – Lola1984 Sep 25 '17 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.