Inplace construct linked list into complete binary tree

Given a linked list which contains N nodes. Our goal is to convert this linked list in equivalent complete binary tree. For example.

Input : 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → NULL

Output : 
               1
              / \
             /   \ 
            /     \
           2       3
          / \     / \
         /   \   /   \
        4     5 6     7 
       / \   /
      8   9 10  

Note given linked list contains 1 data fields and 2 node fields. We can solve this problem using various ways, Best is using of queue and other one is inplace conversion.

This post are based on inplace conversion. Here given code implementation process.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved