A full binary tree is a binary tree where each node has exactly 0 or 2 children.
Return a list of all possible full binary trees with N nodes. Each element of the answer is the root node of one possible tree.
Each node of each tree in the answer must have node.val = 0.
You may return the final list of trees in any order.
分析:
- 显然完全二叉树的节点个数必是奇数,故if N % 2 == 0: return []
- 我们定义cache[i]表示i个节点构造出的完全二叉树的所有可能
- 如果N = 1,显然只有一种情况
- 如果N = 3,显然也只有一种情况
- 如果N = 5,如果左孩子为cache[1],右孩子则为cache[3];如果左孩子为cache[3],右孩子为cache[1]
- 依此类推
1 | class Solution(object): |