Given a Binary Tree, find vertical sum of the nodes that are in same vertical line. Print all sums through different vertical lines. Examples: 1 / \ 2 3 / \ / \ 4 5 6 7 The tree has 5 vertical lines Vertical-Line-1 has only one node 4 => vertical sum is 4 Vertical-Line-2: has only one node 2=> vertical sum is 2 Vertical-Line-3: has three nodes: 1,5,6 => vertical sum is 1+5+6 = 12 Vertical-Line-4: has only one node 3 => vertical sum is 3 Vertical-Line-5: has only one node 7 => vertical sum is 7 So expected output is 4, 2, 12, 3 and 7
500 bananas
ReplyDeleteby stopping at each 1/4th distance i.e. 250 km till it reaches 500 km and then directly sopping at the end.
good navin..
ReplyDeleteI think there is a more optimal solution.....seems like author needs to crosscheck the same !!!
ReplyDeleteyes optimal solution will be 533 more precisely(533.33).
ReplyDeletewhen u have B(banana count) > 2000 & <= 3000; to take the lot 1 Km u need 5 bananas, so when u have traveled 200 Km, B=2000.
Now to take this lot 1 km u need 3 bananas,so when u have traveled 333.33 Km, B=1000.
Now to take this lot u need only 1 banana per kilometer , therefore to travel remaining distance (1000 - 200 -333.33) u need 466.67 bananas
so bananas left is (B - 466.67) = 533.33