use DFS. at any poit of time if u find a node such that it has no left and right child, then you are at leaf. find sum of all values in the stack. for that u can use another stack.
On Oct 7, 4:15 pm, Deepak arora <deepakarora...@gmail.com> wrote: > Please tell the algo of this problem.... > > hasPathSum() > We'll define a "root-to-leaf path" to be a sequence of nodes in a tree > starting with the root node and proceeding > downward to a leaf (a node with no children). We'll say that an empty > tree contains no root-to-leaf paths. So for > example, the following tree has exactly four root-to-leaf paths: > 5 > / \ > 4 8 > / / \ > 11 13 4 > / \ \ > 7 2 1 > Root-to-leaf paths: > path 1: 5 4 11 7 > path 2: 5 4 11 2 > path 3: 5 8 13 > path 4: 5 8 4 1 > For this problem, we will be concerned with the sum of the values of > such a path -- for example, the sum of the > values on the 5-4-11-7 path is 5 + 4 + 11 + 7 = 27. > > thanks.. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@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.