I'm not able to solve this

I checked the office hours video but wasn’t able to understand much. I implemented everything as per that and also did what I thought was there to do.
Still, it’s not working please help.

@theIceman The question is a little broad, can you try and narrow it down a little? Did you not understand approach? Or did some test cases fail? Or is there some performance issue? etc.

I couldn’t get what update_sum is used for and how it’s called and what is it doing.
Do we have to call update_sum for every node after we get the mapping of TreeNode* with map<int, int> or something else.

I hope this helps you.

  1. The problem targets to finding all paths of sum K. If you know all the sum values that occur from the current node to a child node for the left and right branch you can use it to find the number of ways you can generate the sum K around the current root node.
  2. The update_sum would give you a map of the possible sum values from a child to current node and the number of times it would occur. For example consider the tree below :
                                     / \
                                    1  -1
        For this tree:
        update_sum(1): It gives me the possible sum values from a child of 1 to it. 
                       It returns {1:1} meaning 1 occurs once.
        update_sum(-1): It gives me the possible sum values from a child of -1 to it. 
                        It returns {-1:1} meaning -1 occurs once.
        update_sum(2): It gives me the possible sum values from a child of 2 to it. 
                       It returns {2:1,1:1,3:1} meaning 2,3 and 1 occurs once.
                       This is  because there exists a child to 2 path from 1 -> 2
                       which sums to 3, a path from -1 -> 2 which sums to 1 and a
                       path contains only 2 which sums to 2.
1 Like

Thanks, what I was missing is that I also needed to call the function recursively in the count_sum function.

Closing this topic as it has been solved by you yourself. Great job, keep working hard. if its not resolved feel free to un-mark the accepted solution to re-open the topic.