Print path from a node to root of given Complete Binary Tree

11 Jun, 2021
Given an integer N, the task is to find the path from the Nth node to the root of a Binary Tree of the following form:
The Binary Tree is a Complete Binary Tree up to the level of the Nth node.
The nodes are numbered 1 to N, starting from the root as 1.
The structure of the Tree is as follows:  
               1
           /       \
          2         3
       /    \    /   \
      4     5    6    7
      ................
   /    \ ............
 N - 1  N ............
Examples:
Input: N = 7Output: 7 3 1Explanation: The path from the node 7 to root is 7 -> 3 -> 1.
Input: N = 11Output: 11 5 2 1Explanation: The path from node 11 to root is 11 -> 5 -> 2 -> 1.
Naive Approach: The simplest approach to solve the problem is to perform DFS from the given node until the root node is encountered and print the path.
Time Complexity: O(N)Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the structure of the given Binary Tree. It can be observed that for every N, its parent node will be N / 2. Therefore, repeatedly print the current value of N and update N to N / 2 until N is equal to 1, i.e. root node is reached. 
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <iostream>using namespace std; // Function to print the path// from node to rootvoid path_to_root(int node){    // Iterate until root is reached    while (node >= 1) {         // Print the value of        // the current node        cout << node << ' ';         // Move to parent of        // the current node        node /= 2;    }} // Driver Codeint main(){    int N = 7;    path_to_root(N);     return 0;}
// Java program for the above approachimport java.util.*;  class GFG{ // Function to print the path// from node to rootstatic void path_to_root(int node){         // Iterate until root is reached    while (node >= 1)    {                 // Print the value of        // the current node        System.out.print(node + " ");         // Move to parent of        // the current node        node /= 2;    }} // Driver Codepublic static void main(String[] args){    int N = 7;         path_to_root(N);}} // This code is contributed by shivanisinghss2110
# Python3 program for the above approach # Function to print the path# from node to rootdef path_to_root(node):         # Iterate until root is reached    while (node >= 1):         # Print the value of        # the current node        print(node, end = " ")         # Move to parent of        # the current node        node //= 2 # Driver Codeif __name__ == '__main__':     N = 7     path_to_root(N) # This code is contributed by mohit kumar 29
// C# program for the above approachusing System;class GFG{ // Function to print the path// from node to rootstatic void path_to_root(int node){         // Iterate until root is reached    while (node >= 1)    {                 // Print the value of        // the current node        Console.Write(node + " ");         // Move to parent of        // the current node        node /= 2;    }} // Driver Codepublic static void Main(String[] args){    int N = 7;       path_to_root(N);}} // This code is contributed by shivanisinghss2110
<script> // Javascript program for the above approach // Function to print the path// from node to rootfunction path_to_root(node){         // Iterate until root is reached    while (node >= 1)    {                 // Print the value of        // the current node        document.write(node + " ");         // Move to parent of        // the current node        node = parseInt(node / 2, 10);    }} // Driver codelet N = 7; path_to_root(N); // This code is contributed by divyeshrabadiya07 </script>
7 3 1
 
Time Complexity: O(log2(N))Auxiliary Space: O(1)
mohit kumar 29
shivanisinghss2110
nidhi_biet
divyeshrabadiya07
DFS
interview-preparation
Numbers
Greedy
Mathematical
Tree
Greedy
Mathematical
DFS
Numbers
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Policemen catch thieves
K Centers Problem | Set 1 (Greedy Approximate Algorithm)
Maximize difference between the Sum of the two halves of the Array after removal of N elements
Minimize Cash Flow among a given set of friends who have borrowed money from each other
Minimum time taken by each job to be completed given by a Directed Acyclic Graph
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
C++ Data Types
Merge two sorted arrays
Operators in C / C++