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
Hint: use induction approach before reaching nth stair he can be either at n-1 or n-2 stairs.
ReplyDeleteSuppose W(n) different ways are there two reach at top of stair.
ReplyDeleteNow
W(1) = 1 [It can be done by a single step only]
W(2) = 2 [It can be done by taking two stairs at one time or 2 steps of one stair]
W(3) = W(2) + W(1) [1st reach upto stair #1 and then move to stair #3 by taking double step or
1st reach upto stair #2 and then move to stair #3 by taking single step]
Similarly W(n) = W(n-1) + W(n-2)
@Hsingh you are right.The sequence generated is like fibonacci series.
ReplyDelete