You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree - it does not have to start at the root.
Design an algorithm to print all paths which sum up to that value
You need to traverse every node to be sure that it is or not a path. So the complexity can't be lower than O(n).
About your algorithm.
Starting in every node you traverse all the tree that has that node as root. In a balanced tree that means one half of the number that the parent has. n + 2 * n/2 + 4 *n/4 + ...
But you only have lg(n) levels, where every level adds up to n, so your complexity is O(n lg(n) )
I dunno how to improve that. Besides, how many paths there are (as a function of n)?