You are given a tree,number X and each node have a number on it.You have to find some nodes a1,a2,a3 ... ,an such a2 is child of a1 ,a3 is child of a2,...an is child of an - 1 and a1 + a2 + ... + an closest number to X.We say x is child of y if it's in subtree with root in y. This problem was on Serbian state competition 2014. for seniors,could you give me any hints or pseudocode?
I have an Idea But i'm not Sure about it ,i hope it helps you. Do a Dfs , and save the sum of all nodes in an array called depth for exemple , and for each node do a Binery search in the depth array to find the closest parent a1 such sum from a1 to current node is X.