And of course boundary cases(leaf nodes) are to be handled. For a leaf node 'i', ok[i][j]=1(if j==v[i]), 0 otherwise!!!
On Dec 28, 11:04 pm, suhash <suhash.venkat...@gmail.com> wrote: > I think this can be solved using dp. Consider the subtree rooted at > node 'i'. Let ok[i][j] be a boolean (0 or 1) denoting whether you can > achieve a sum of 'j', with the subtree rooted at node 'i', and node > 'i' is chosen in the path. > > Hence, ok[i][j] = max((k=0 to (j-v[i])) ok[left(i)][k]&ok[right(i)][j- > v[i]-k]) > This can be computed in a bottom up fashion for each node(each 'i') > and for each possible sum(each 'j'). Hence, complexity is O(n*s*s). > > Now, if input is S (given sum value to find) > Then, for each node 'i' if ok[i][S]=1, then a path exists with this > node as root. > > After this, printing all paths can be done easily again similar to the > first case. > > On Dec 26, 10:08 pm, MAC <macatad...@gmail.com> wrote: > > > you are given a bst where each node has a int value , parent pointer , and > > left and right pointers , write a function to find a path with a given sum > > value. Path can go from left subtree tree , include root and go to right > > tree as well . we need to find these paths also . > > > 5 > > 1 10 > > 0 2 6 11 > > > so to find 16 we say it is 1 to 5 to 10 > > > -- > > thanks > > --mac -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.