-3

Given a Binary Tree print all paths that leads to a given sum (s) . Note : In this problem the sum can be in part of left and right sub-tree as well. Path need not to start at root.

Though i refer this question for help. Still i could not come up for solution for a special case where path For sum:23, shown path should come as ouputinclude left and right sub-tree as well.

If given sum 23 algorithm should return path as highlighted in picture above.

Community
  • 1
  • 1
Abhay Kumar
  • 105
  • 1
  • 5
  • Two pieces of advice. First, you explain your question, but do not show how you have tried to answer it, please add this. Second, before you write code for an algorithm, you should determine the algorithm. Solve the problem in theory before wasting time with code. – Aaron3468 May 09 '16 at 07:22
  • @Aaron3468 thanks for advice. The question was asked with genuine interest. I will update the question. – Abhay Kumar May 10 '16 at 16:34

1 Answers1

0

Keep in mind for the future that an expectation of using the site is that you show your attempts to solve for yourself. In particular, stack overflow tends to nuclear downvote, flag, and delete any attempts to seek help without showing genuine effort. I support this tendency because most people first arriving here want stack overflow to code for them, rather than support their coding.

Moving along, It's a really simple algorithm and I'm providing it in good faith that you are here to learn, and not for free labour.

for each node in tree
    for each child node 
        add the child to an existing path and add the path to list of candidates
        if sum(nodes in path) is target sum
            add path to list of solutions
        repeat expansion until no child nodes remain
Aaron3468
  • 1,734
  • 16
  • 29