Input: 0 / \ 1 1 / \ / \ 1 10 70 1 / \ 81 2 Output: 2 Explanation: There are 2 Fibonacci path for the above Binary Tree, for x = 3, Path 1: 0 1 1 Path 2: 0 1 1 2 Input: 8 / \ 4 81 / \ / \ 3 2 70 243 / \ 81 909 Output: 0
Approach: The idea is to use Preorder Tree Traversal. During preorder traversal of the given binary tree do the following:
- First calculate the Height of binary tree .
- Now create a vector of length equals height of tree, such that it contains Fibonacci Numbers.
- If current value of the node at ith level is not equal to ith term of fibonacci series or pointer becomes NULL then return the count.
- If the current node is a leaf node then increment the count by 1.
- Recursively call for the left and right subtree with the updated count.
- After all recursive call, the value of count is number of Fibonacci paths for a given binary tree.
Below is the implementation of the above approach:
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.
- Count even paths in Binary Tree
- Count all k-sum paths in a Binary Tree
- Count of exponential paths in a Binary Tree
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of root to leaf paths in a Binary Tree that form an AP
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Print all k-sum paths in a binary tree
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print all the paths from root, with a specified sum in Binary tree
- Print Palindromic Paths of Binary tree
- Given a binary tree, print all root-to-leaf paths
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Root to leaf paths having equal lengths in a Binary Tree
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Count nodes in the given tree whose weight is a fibonacci number
- Count the Number of Binary Search Trees present in a Binary Tree
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Count of subtrees in a Binary Tree having XOR value K
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.