Find the numbers present at Kth level of a Fibonacci Binary Tree
Given a number K, the task is to print the fibonacci numbers present at Kth level of a Fibonacci Binary Tree.
Examples:
Input: K = 3
Output: 2, 3, 5, 8
Explanation:
Fibonacci Binary Tree for 3 levels:
0
/ \
1 1
/\ / \
2 3 5 8
Numbers present at level 3: 2, 3, 5, 8
Input: K = 2
Output: 1, 1
Explanation:
Fibonacci Binary Tree for 2 levels:
0
/ \
1 1
Numbers present at level 2: 1, 1
Naive Approach: The naive approach is to build a Fibonacci Binary Tree (binary tree of Fibonacci numbers) and then get elements at a particular level K.
However, this approach is obsolete for large numbers as it takes too much time.
Efficient approach: Since the elements which would be present at some arbitrary level K of the tree can be found by finding the elements in the range [2K – 1, 2K – 1]. Therefore:
- Find the Fibonacci numbers up to 106 using Dynamic Programming and store them in an array.
- Calculate the left_index and right_index of the level as:
left_index = 2K - 1 right_index = 2K - 1
- Print the fibonacci numbers from left_index to right_index of fibonacci array.
Below is the implementation of the above approach:
C++
// C++ program to print the Fibonacci numbers// present at K-th level of a Binary Tree #include <bits/stdc++.h>using namespace std; // Initializing the max value#define MAX_SIZE 100005 // Array to store all the// fibonacci numbersint fib[MAX_SIZE + 1]; // Function to generate fibonacci numbers// using Dynamic Programmingvoid fibonacci(){ int i; // 0th and 1st number of the series // are 0 and 1 fib[0] = 0; fib[1] = 1; for (i = 2; i <= MAX_SIZE; i++) { // Add the previous two numbers in the // series and store it fib[i] = fib[i - 1] + fib[i - 2]; }} // Function to print the Fibonacci numbers// present at Kth level of a Binary Treevoid printLevel(int level){ // Finding the left and right index int left_index = pow(2, level - 1); int right_index = pow(2, level) - 1; // Iterating and printing the numbers for (int i = left_index; i <= right_index; i++) { cout << fib[i - 1] << " "; } cout << endl;} // Driver codeint main(){ // Precomputing Fibonacci numbers fibonacci(); int K = 4; printLevel(K); return 0;} |
Java
// Java program to print the Fibonacci numbers// present at K-th level of a Binary Treeimport java.util.*; class GFG{ // Initializing the max valuestatic final int MAX_SIZE = 100005; // Array to store all the// fibonacci numbersstatic int []fib = new int[MAX_SIZE + 1]; // Function to generate fibonacci numbers// using Dynamic Programmingstatic void fibonacci(){ int i; // 0th and 1st number of the series // are 0 and 1 fib[0] = 0; fib[1] = 1; for (i = 2; i <= MAX_SIZE; i++) { // Add the previous two numbers in the // series and store it fib[i] = fib[i - 1] + fib[i - 2]; }} // Function to print the Fibonacci numbers// present at Kth level of a Binary Treestatic void printLevel(int level){ // Finding the left and right index int left_index = (int) Math.pow(2, level - 1); int right_index = (int) (Math.pow(2, level) - 1); // Iterating and printing the numbers for (int i = left_index; i <= right_index; i++) { System.out.print(fib[i - 1]+ " "); } System.out.println();} // Driver codepublic static void main(String[] args){ // Precomputing Fibonacci numbers fibonacci(); int K = 4; printLevel(K);}} // This code is contributed by Rajput-Ji |
Python3
# Python program to print the Fibonacci numbers# present at K-th level of a Binary Tree # Initializing the max valueMAX_SIZE = 100005 # Array to store all the# fibonacci numbersfib =[0]*(MAX_SIZE + 1) # Function to generate fibonacci numbers# using Dynamic Programmingdef fibonacci(): # 0th and 1st number of the series # are 0 and 1 fib[0] = 0 fib[1] = 1 for i in range(2, MAX_SIZE + 1): # Add the previous two numbers in the # series and store it fib[i] = fib[i - 1] + fib[i - 2] # Function to print the Fibonacci numbers# present at Kth level of a Binary Treedef printLevel(level): # Finding the left and right index left_index = pow(2, level - 1) right_index = pow(2, level) - 1 # Iterating and printing the numbers for i in range(left_index, right_index+1): print(fib[i - 1],end=" ") print() # Driver code # Precomputing Fibonacci numbersfibonacci() K = 4printLevel(K) # This code is contributed by shivanisinghss2110 |
C#
// C# program to print the Fibonacci numbers // present at K-th level of a Binary Tree using System; class GFG{ // Initializing the max value static int MAX_SIZE = 100005; // Array to store all the // fibonacci numbers static int []fib = new int[MAX_SIZE + 1]; // Function to generate fibonacci numbers // using Dynamic Programming static void fibonacci() { int i; // 0th and 1st number of the series // are 0 and 1 fib[0] = 0; fib[1] = 1; for (i = 2; i <= MAX_SIZE; i++) { // Add the previous two numbers in the // series and store it fib[i] = fib[i - 1] + fib[i - 2]; } } // Function to print the Fibonacci numbers // present at Kth level of a Binary Tree static void printLevel(int level) { // Finding the left and right index int left_index = (int) Math.Pow(2, level - 1); int right_index = (int) (Math.Pow(2, level) - 1); // Iterating and printing the numbers for (int i = left_index; i <= right_index; i++) { Console.Write(fib[i - 1]+ " "); } Console.WriteLine(); } // Driver code public static void Main(string[] args) { // Precomputing Fibonacci numbers fibonacci(); int K = 4; printLevel(K); } } // This code is contributed by Yash_R |
Javascript
<script> // Javascript program to prvar the Fibonacci numbers// present at K-th level of a Binary Tree // Initializing the max value var MAX_SIZE = 100005; // Array to store all the // fibonacci numbers var fib = Array(MAX_SIZE + 1).fill(0); // Function to generate fibonacci numbers // using Dynamic Programming function fibonacci() { var i; // 0th and 1st number of the series // are 0 and 1 fib[0] = 0; fib[1] = 1; for (i = 2; i <= MAX_SIZE; i++) { // Add the previous two numbers in the // series and store it fib[i] = fib[i - 1] + fib[i - 2]; } } // Function to print the Fibonacci numbers // present at Kth level of a Binary Tree function printLevel(level) { // Finding the left and right index var left_index = parseInt( Math.pow(2, level - 1)); var right_index = parseInt( (Math.pow(2, level) - 1)); // Iterating and printing the numbers for (i = left_index; i <= right_index; i++) { document.write(fib[i - 1] + " "); } document.write(); } // Driver code // Precomputing Fibonacci numbers fibonacci(); var K = 4; printLevel(K); // This code contributed by umadevi9616 </script> |
13 21 34 55 89 144 233 377
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


