![]() |
VOOZH | about |
Given a Binary Tree, the task is to count the number of Fibonacci paths in the given Binary Tree.
Fibonacci Path is a path which contains all nodes in root to leaf path are terms of Fibonacci series.
Example:
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:
Below is the implementation of the above approach:
2
Time Complexity: O(n), where n is the number of nodes in the given tree.
Auxiliary Space: O(h), where h is the height of the tree.