简体   繁体   English

高度为h可以形成多少种不同的二叉树?

[英]How many types of distinct Binary Tree can be formed with a height of h?

How many types of distinct Binary Tree can be formed with a height of h? 高度为h可以形成多少种不同的二叉树? if we only know the height of binary tree, and we regard root-left and root-right as the same tree structure, this means if the height of binary tree equal to 1, we can formed 2 different trees structure: root - leftchild/rightchild; 如果我们只知道二叉树的高度,并且将root-left和root-right视为相同的树结构,这意味着如果二叉树的高度等于1,我们可以形成2种不同的树结构:root-leftchild /权子 root - leftchild - rightchild 根-左子-右子

Let's there are S[h] ways to build a tree with a height of h ( (h)-tree ). 让我们有S [h]种方法来构建高度为h的(h)-tree )。
We can compose (h-tree) from root node and from: 我们可以从根节点和以下位置组成(h树):
- left (h-1)-tree and any right tree with height 0..h-2 -左(h-1)树和任何高度为0..h-2的右树
- two (h-1)-trees -两棵(h-1)树
- right (h-1)-tree and any left tree with height 0..h-2 -右(h-1)树和任何高度为0..h-2的左树

So recursive formula is 所以递归公式是

 S[h] = S[h-1] * Sum{k=0..h-2} (S[k]) + 
        S[h-1] * S[h-1] + 
        S[h-1] * Sum{k=0..h-2} (S[k]) = 
        S[h-1] * (S[h-1]  + 2 * Sum{k=0..h-2} (S[k]))

Now with S[0] = 1, S[1] = 1 we can find all values with recursion. 现在,当S[0] = 1, S[1] = 1我们可以递归找到所有值。

Example: S[3] = S[2] * (S[2] + 2*(S[0] + S[1])) = 3 * (3 + 2 * (1 + 1)) = 21 例如: S[3] = S[2] * (S[2] + 2*(S[0] + S[1])) = 3 * (3 + 2 * (1 + 1)) = 21

Note that the same values will be computed again and again, so it is worth to use dynamic programming: 请注意,将一次又一次地计算相同的值,因此值得使用动态编程:
- or top-down memoization - when we write once calculated value S[k] to the table and use it later, it is very simple modification of recursion -或自上而下的memoization -当我们向表中写入一次计算值S [k]并在以后使用时,这是对递归的非常简单的修改
- or bottom-up approach - here we can use additional table with cumulative sums. -或自下而上的方法-在这里,我们可以使用带有累加总和的附加表。

To check: S[5] = 457653 检查: S[5] = 457653

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM