📜  给定预排序序列长度的二叉树数

📅  最后修改于: 2021-06-25 17:06:18             🧑  作者: Mango

对给定的预序列长度为n的二叉树数目进行计数。
例子:

Input : n = 1
Output : 1

Input : n = 2
Output : 2

Input : n = 3
Output : 5

背景 :

在预遍历中,我们先处理根节点,然后遍历左子节点,然后遍历右子节点。
例如,下面的树的遍历遍历为1 2 4 5 3 6 7

查找具有给定预购的树木数量:

如果给出了这样的遍历长度(假设为n),则可能为二叉树的数量。
让我们举个例子:给定的预购顺序–> 2 4 6 8 10(长度5)。

  • 假设只有1个节点(在这种情况下为2个节点),那么仅可能有1个二叉树
  • 现在,假设有2个节点(分别是2和4),那么只有2个二叉树是可能的:
  • 现在,当有3个节点(即2、4和6)时,所以可能的二叉树为5
  • 考虑4个节点(分别是2、4、6和8),因此可能的二叉树为14。
    假设BT(1)表示1个节点的二叉树数。 (我们假设BT(0)= 1)
    BT(4) = BT(0)* BT(3)+ BT(1)* BT(2)+ BT(2)* BT(1)+ BT(3)* BT(0)
    BT(4) = 1 * 5 +1 * 2 + 2 * 1 + 5 * 1 = 14
  • 同样,考虑所有5个节点(2、4、6、8和10)。二叉树的可能数目为:
    BT(5) = BT(0)* BT(4)+ BT(1)* BT(3)+ BT(2)* BT(2)+ BT(3)* BT(1)+ BT(4)* BT(0)
    BT(5) = 1 * 14 +1 * 5 + 2 * 2 + 5 * 1 + 14 * 1 = 42

因此,长度为5的预排序序列的总二叉树为42。
我们使用动态编程来计算可能的二叉树数量。我们一次取一个节点,并使用先前计算的树来计算可能的树。

C++
// C++ Program to count possible binary trees
// using dynamic programming
#include 
using namespace std;
 
int countTrees(int n)
{
    // Array to store number of Binary tree
    // for every count of nodes
    int BT[n + 1];
    memset(BT, 0, sizeof(BT));
 
    BT[0] = BT[1] = 1;
 
    // Start finding from 2 nodes, since
    // already know for 1 node.
    for (int i = 2; i <= n; ++i)
        for (int j = 0; j < i; j++)
            BT[i] += BT[j] * BT[i - j - 1];
 
    return BT[n];
}
 
// Driver code
int main()
{
    int n = 5;
    cout << "Total Possible Binary Trees are : "
        << countTrees(n) << endl;
    return 0;
}


Java
// Java Program to count
// possible binary trees
// using dynamic programming
import java.io.*;
 
class GFG
{
static int countTrees(int n)
{
    // Array to store number
    // of Binary tree for
    // every count of nodes
    int BT[] = new int[n + 1];
    for(int i = 0; i <= n; i++)
    BT[i] = 0;
    BT[0] = BT[1] = 1;
 
    // Start finding from 2
    // nodes, since already
    // know for 1 node.
    for (int i = 2; i <= n; ++i)
        for (int j = 0; j < i; j++)
            BT[i] += BT[j] *
                     BT[i - j - 1];
 
    return BT[n];
}
 
// Driver code
public static void main (String[] args)
{
int n = 5;
System.out.println("Total Possible " +
                "Binary Trees are : " +
                       countTrees(n));
}
}
 
// This code is contributed by anuj_67.


Python3
# Python3 Program to count possible binary
# trees using dynamic programming
 
def countTrees(n) :
 
    # Array to store number of Binary
    # tree for every count of nodes
    BT = [0] * (n + 1)
 
    BT[0] = BT[1] = 1
 
    # Start finding from 2 nodes, since
    # already know for 1 node.
    for i in range(2, n + 1):
        for j in range(i):
            BT[i] += BT[j] * BT[i - j - 1]
 
    return BT[n]
 
# Driver Code
if __name__ == '__main__':
 
    n = 5
    print("Total Possible Binary Trees are : ",
                                 countTrees(n))
                                  
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# Program to count
// possible binary trees
// using dynamic programming
using System;
 
class GFG
{
static int countTrees(int n)
{
    // Array to store number
    // of Binary tree for
    // every count of nodes
    int []BT = new int[n + 1];
    for(int i = 0; i <= n; i++)
        BT[i] = 0;
        BT[0] = BT[1] = 1;
 
    // Start finding from 2
    // nodes, since already
    // know for 1 node.
    for (int i = 2; i <= n; ++i)
        for (int j = 0; j < i; j++)
            BT[i] += BT[j] *
                     BT[i - j - 1];
 
    return BT[n];
}
 
// Driver code
static public void Main (String []args)
{
    int n = 5;
    Console.WriteLine("Total Possible " +
                      "Binary Trees are : " +
                             countTrees(n));
}
}
 
// This code is contributed
// by Arnab Kundu


PHP


Javascript


输出:

Total Possible Binary Trees are : 42