June 2022 C/C++ (Level 7) Real Exam Questions Analysis #中国 Electronics Society # National Youth Software Programming Level Examination

Insert image description here

C/C++ Programming (Levels 1~8) All real questions・Click here

Question 1: How many types of binary trees are there?

Input n (1<n<13), find how many forms of binary tree with n nodes there are.
Time limit: 1000
Memory limit: 65536
input
Integer n
output
Answer
sample input
3
Sample output
5

This problem can be solved using dynamic programming methods. We can define an array dpthat dp[i]represents ithe number of binary tree types when there is a node. According to the properties of binary trees, we can know that the number of species of a binary tree depends on the species of its left subtree and right subtree.

The specific dynamic programming recursion relationship is as follows:

For ia binary tree of nodes, we can select a node as the root node, and the nodes to its left form the left subtree, and the nodes to its right form the right subtree. According to this division, the following relationship can be obtained:

dp[i] = dp[0] * dp[i-1] + dp[1] * dp[i-2] + ... + dp[i-1] * dp[0]

in,

Guess you like

Origin blog.csdn.net/gozhuyinglong/article/details/132655355