@mac ..ACTUAL QUESTION IS like this Given a binary tree and a number, return true if the tree has a root- to-leaf path such that adding up all the values along the path equals the given number. Return false if no such path can be found. so given tree is like this
12 so possible combination are 12->7->3 / \ 12->7- >4 7 6 12->6->6 / \ / 3 4 6 So the returned value should be true only for numbers 22, 23 and 24. For any other number, returned value should be false. int hasPathfromRoot_leaf(struct node* node, int sum) //same logic what ashish provided .. { return true if we run out of tree and sum==0 if (node == NULL) { return 0; } else { otherwise check both subtrees int subSum = sum - node->data; return (hasPathfromRoot_leaf(node->left, subSum) || hasPathfromRoot_leaf(node->right, subSum)); } } Thanks & Regards Shashank Mani " Don't B Evil U Can Earn While U Learn" -- 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.