KoustavBeginner's blog

By KoustavBeginner, history, 7 weeks ago, In English

question:https://www.hackerrank.com/contests/hack-the-interview-vi-asia-pacific/challenges/constructing-a-tree/problem

i have tried a lot but not able to construct the tree. i am not able to understand the code please can any one make me understand in easy language.

 
 
 
 
  • Vote: I like it
  • +2
  • Vote: I do not like it

»
7 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

The solution is into $$$3$$$ parts:

  1. Making straight chain, so $$$1$$$ node each at a height of $$$0,1,\ldots,p$$$.

  2. Adding $$$n-p-2$$$ nodes at height $$$1$$$.

  3. Adding the remaining $$$1$$$ node at height which satisfy the score constraint.

Do read the tester's code instead, my code was an alternate lengthy solution.

  • »
    »
    7 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    ya but for testcase 5 6 we can get maximum p=3 ans no. of nodes till now is=4; we are left with one node and remaining length we need is 0; but where to add one extra node.