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
we will start with both the hour-glasses and
ReplyDeleteas soon as the sand flows to the other bulb
we will invert it.this inversion process will be done three times for both the hourglss.
we start measuring time after the third inversion of 4-min hourglass and measure it till the complete sand flow of 7-min hourglass.
time= 7*3-4*3 = 9 minutes.
@navin good approach.you are using total of 6 inverts.you can improve it.
ReplyDeleteTime 4m timer 7 minute timer
ReplyDelete0 min Start Start
4 mins Flip 3 minutes left
7 mins 1 m left Flip
8 mins Stop Flip (1 minute left)
9 mins Stop
In this approach only 3 inverts are required.