📌  相关文章
📜  二叉树中斐波那契路径的数量

📅  最后修改于: 2021-09-22 09:44:34             🧑  作者: Mango

给定一个二叉树,任务是计算给定二叉树中斐波那契路径的数量。

例子:

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

方法:想法是使用 Preorder Tree Traversal。在给定二叉树的前序遍历期间,请执行以下操作:

  1. 首先计算二叉树的高度。
  2. 现在创建一个长度等于树高的向量,使其包含斐波那契数。
  3. 如果i 层节点的当前值不等于斐波那契数列的i 项或指针变为NULL,则返回计数。
  4. 如果当前节点是叶节点,则将计数加 1。
  5. 递归调用与更新的计子树。
  6. 在全递归调用之后,count 的值是给定二叉树的斐波那契路径数。

下面是上述方法的实现:

C++
// C++ program to count all of
// Fibonacci paths in a Binary tree
 
#include 
using namespace std;
 
// Vector to store the fibonacci series
vector fib;
 
// Binary Tree Node
struct node {
    struct node* left;
    int data;
    struct node* right;
};
 
// Function to create a new tree node
node* newNode(int data)
{
    node* temp = new node;
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
 
// Function to find the height
// of the given tree
int height(node* root)
{
    int ht = 0;
    if (root == NULL)
        return 0;
 
    return (max(height(root->left),
                height(root->right))
            + 1);
}
 
// Function to make fibonacci series
// upto n terms
void FibonacciSeries(int n)
{
    fib.push_back(0);
    fib.push_back(1);
    for (int i = 2; i < n; i++)
        fib.push_back(fib[i - 1]
                      + fib[i - 2]);
}
 
// Preorder Utility function to count
// exponent path in a given Binary tree
int CountPathUtil(node* root,
                  int i, int count)
{
 
    // Base Condition, when node pointer
    // becomes null or node value is not
    // a number of pow(x, y )
    if (root == NULL
        || !(fib[i] == root->data)) {
        return count;
    }
 
    // Increment count when
    // encounter leaf node
    if (!root->left
        && !root->right) {
        count++;
    }
 
    // Left recursive call
    // save the value of count
    count = CountPathUtil(
        root->left, i + 1, count);
 
    // Right recursive call and
    // return value of count
    return CountPathUtil(
        root->right, i + 1, count);
}
 
// Function to find whether
// fibonacci path exists or not
void CountPath(node* root)
{
    // To find the height
    int ht = height(root);
 
    // Making fibonacci series
    // upto ht terms
    FibonacciSeries(ht);
 
    cout << CountPathUtil(root, 0, 0);
}
 
// Driver code
int main()
{
    // Create binary tree
    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);
 
    // Function Call
    CountPath(root);
 
    return 0;
}


Java
// Java program to count all of
// Fibonacci paths in a Binary tree
import java.util.*;
 
class GFG{
 
// Vector to store the fibonacci series
static Vector fib = new Vector();
 
// Binary Tree Node
static class node {
    node left;
    int data;
    node right;
};
 
// Function to create a new tree node
static node newNode(int data)
{
    node temp = new node();
    temp.data = data;
    temp.left = null;
    temp.right = null;
    return temp;
}
 
// Function to find the height
// of the given tree
static int height(node root)
{
    if (root == null)
        return 0;
 
    return (Math.max(height(root.left),
                height(root.right))
            + 1);
}
 
// Function to make fibonacci series
// upto n terms
static void FibonacciSeries(int n)
{
    fib.add(0);
    fib.add(1);
    for (int i = 2; i < n; i++)
        fib.add(fib.get(i - 1)
                    + fib.get(i - 2));
}
 
// Preorder Utility function to count
// exponent path in a given Binary tree
static int CountPathUtil(node root,
                int i, int count)
{
 
    // Base Condition, when node pointer
    // becomes null or node value is not
    // a number of Math.pow(x, y )
    if (root == null
        || !(fib.get(i) == root.data)) {
        return count;
    }
 
    // Increment count when
    // encounter leaf node
    if (root.left != null
        && root.right != null) {
        count++;
    }
 
    // Left recursive call
    // save the value of count
    count = CountPathUtil(
        root.left, i + 1, count);
 
    // Right recursive call and
    // return value of count
    return CountPathUtil(
        root.right, i + 1, count);
}
 
// Function to find whether
// fibonacci path exists or not
static void CountPath(node root)
{
    // To find the height
    int ht = height(root);
 
    // Making fibonacci series
    // upto ht terms
    FibonacciSeries(ht);
 
    System.out.print(CountPathUtil(root, 0, 0));
}
 
// Driver code
public static void main(String[] args)
{
    // Create binary tree
    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);
 
    // Function Call
    CountPath(root);
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to count all of
# Fibonacci paths in a Binary tree
  
# Vector to store the fibonacci series
fib = []
  
# Binary Tree Node
class node:
     
    def __init__(self, data):
         
        self.data = data
        self.left = None
        self.right = None
  
# Function to create a new tree node
def newNode(data):
 
    temp = node(data)
    return temp
  
# Function to find the height
# of the given tree
def height(root):
 
    ht = 0
     
    if (root == None):
        return 0
  
    return (max(height(root.left),
                height(root.right)) + 1)
 
# Function to make fibonacci series
# upto n terms
def FibonacciSeries(n):
 
    fib.append(0)
    fib.append(1)
     
    for i in range(2, n):
        fib.append(fib[i - 1] + fib[i - 2])
 
# Preorder Utility function to count
# exponent path in a given Binary tree
def CountPathUtil(root, i, count):
  
    # Base Condition, when node pointer
    # becomes null or node value is not
    # a number of pow(x, y )
    if (root == None or not (fib[i] == root.data)):
        return count
     
    # Increment count when
    # encounter leaf node
    if (not root.left and not root.right):
        count += 1
  
    # Left recursive call
    # save the value of count
    count = CountPathUtil(root.left, i + 1, count)
  
    # Right recursive call and
    # return value of count
    return CountPathUtil(root.right, i + 1, count)
 
# Function to find whether
# fibonacci path exists or not
def CountPath(root):
 
    # To find the height
    ht = height(root)
  
    # Making fibonacci series
    # upto ht terms
    FibonacciSeries(ht)
     
    print(CountPathUtil(root, 0, 0))
 
# Driver code
if __name__=='__main__':
 
    # Create binary tree
    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)
  
    # Function Call
    CountPath(root)
  
# This code is contributed by rutvik_56


C#
// C# program to count all of
// Fibonacci paths in a Binary tree
using System;
using System.Collections.Generic;
 
class GFG{
  
// List to store the fibonacci series
static List fib = new List();
  
// Binary Tree Node
class node {
    public node left;
    public int data;
    public node right;
};
  
// Function to create a new tree node
static node newNode(int data)
{
    node temp = new node();
    temp.data = data;
    temp.left = null;
    temp.right = null;
    return temp;
}
  
// Function to find the height
// of the given tree
static int height(node root)
{
    if (root == null)
        return 0;
  
    return (Math.Max(height(root.left),
                height(root.right))
            + 1);
}
  
// Function to make fibonacci series
// upto n terms
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]);
}
  
// Preorder Utility function to count
// exponent path in a given Binary tree
static int CountPathUtil(node root,
                int i, int count)
{
  
    // Base Condition, when node pointer
    // becomes null or node value is not
    // a number of Math.Pow(x, y)
    if (root == null
        || !(fib[i] == root.data)) {
        return count;
    }
  
    // Increment count when
    // encounter leaf node
    if (root.left != null
        && root.right != null) {
        count++;
    }
  
    // Left recursive call
    // save the value of count
    count = CountPathUtil(
        root.left, i + 1, count);
  
    // Right recursive call and
    // return value of count
    return CountPathUtil(
        root.right, i + 1, count);
}
  
// Function to find whether
// fibonacci path exists or not
static void CountPath(node root)
{
    // To find the height
    int ht = height(root);
  
    // Making fibonacci series
    // upto ht terms
    FibonacciSeries(ht);
  
    Console.Write(CountPathUtil(root, 0, 0));
}
  
// Driver code
public static void Main(String[] args)
{
    // Create binary tree
    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);
  
    // Function Call
    CountPath(root);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程