Find the numbers present at Kth level of a Fibonacci Binary Tree | C ++ program to print the Fibonacci numbers present at K - th level of a Binary Tree ; Initializing the max value ; Array to store all the fibonacci numbers ; Function to generate fibonacci numbers using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous two numbers in the series and store it ; Function to print the Fibonacci numbers present at Kth level of a Binary Tree ; Finding the left and right index ; Iterating and printing the numbers ; Driver code ; Precomputing Fibonacci numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_SIZE  100005 NEW_LINE int fib [ MAX_SIZE + 1 ] ; void fibonacci ( ) { int i ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( i = 2 ; i <= MAX_SIZE ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } void printLevel ( int level ) { int left_index = pow ( 2 , level - 1 ) ; int right_index = pow ( 2 , level ) - 1 ; for ( int i = left_index ; i <= right_index ; i ++ ) { cout << fib [ i - 1 ] << " ▁ " ; } cout << endl ; } int main ( ) { fibonacci ( ) ; int K = 4 ; printLevel ( K ) ; return 0 ; }