Given a number K, the task is to print the fibonacci numbers present at Kth level of a Fibonacci Binary Tree.
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:
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.
- Prime Numbers present at Kth level of a Binary Tree
- Sum of numbers in the Kth level of a Fibonacci triangle
- Sum of all nodes at Kth level in a Binary Tree
- Nodes at Kth level without duplicates in a Binary Tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Count the Number of Binary Search Trees present in a Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Print nodes between two given level numbers of a binary tree
- Find the kth node in vertical order traversal of a Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Swap Kth node from beginning with Kth node from end in a Linked List
- Find maximum level sum in Binary Tree
- Find if given vertical level of binary tree is sorted or not
- Find maximum level product in Binary Tree
- Find the maximum node at a given level in a binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.