## bhupi

BAN USERGiven tree is a complete Binary Tree. We can take advantage of complete binary tree properties.

Keep a count of element in the tree. Based on the count of elements we can find path. Lets say path for root element is "0".

Path for second element will be "0 L"

For Third "0 R"

For Fourth "0 L L"

For Fifth "0 L R"

Logic:

Lets say we need to find path for 7th element.

Find level of seventh element. Its 3

Now find number of elment on its previous level - Its 4

Now Path of 7th element - path of '((7 - 4)th element + "R"'

So if we need to find path for nth element and its level is 'l'.

Formula for path will be :- path of ( n - pow(2, l -1))th element + (n % 2 == 0 ? 'L' : 'R')

Note: u need to small modification in algo to handle corner cases

Rep**valeriecfranks**, Computer Scientist at ASAPInfosystemsPvtLtdA strong writer with a passion for story-telling who has extensive experience of writing literary compositions, articles, reports, books and ...

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

- bhupi May 14, 2017