using
System;
using
System.Collections.Generic;
class
GFG{
static
List<
int
> fib =
new
List<
int
>();
class
node {
public
node left;
public
int
data;
public
node right;
};
static
node newNode(
int
data)
{
node temp =
new
node();
temp.data = data;
temp.left =
null
;
temp.right =
null
;
return
temp;
}
static
int
height(node root)
{
if
(root ==
null
)
return
0;
return
(Math.Max(height(root.left),
height(root.right))
+ 1);
}
static
void
FibonacciSeries(
int
n)
{
fib.Add(0);
fib.Add(1);
for
(
int
i = 2; i < n; i++)
fib.Add(fib[i - 1]
+ fib[i - 2]);
}
static
int
CountPathUtil(node root,
int
i,
int
count)
{
if
(root ==
null
|| !(fib[i] == root.data)) {
return
count;
}
if
(root.left !=
null
&& root.right !=
null
) {
count++;
}
count = CountPathUtil(
root.left, i + 1, count);
return
CountPathUtil(
root.right, i + 1, count);
}
static
void
CountPath(node root)
{
int
ht = height(root);
FibonacciSeries(ht);
Console.Write(CountPathUtil(root, 0, 0));
}
public
static
void
Main(String[] args)
{
node root = newNode(0);
root.left = newNode(1);
root.right = newNode(1);
root.left.left = newNode(1);
root.left.right = newNode(4);
root.right.right = newNode(1);
root.right.right.left = newNode(2);
CountPath(root);
}
}