Search code examples
algorithmbinary-treecatalan

generate all structurally distinct full binary trees with n leaves


This is a homework, I have difficulties in thinking of it. Please give me some ideas on recursions and DP solutions. Thanks a lot

generate and print all structurally distinct full binary trees with n leaves in dotted parentheses form, "full" means all internal (non-leaf) nodes have exactly two children.

For example, there are 5 distinct full binary trees with 4 leaves each.


Solution

  • U can use recursion, on i-th step u consider i-th level of tree and u chose which nodes will be present on this level according to constraints: - there is parent on previous level - no single children present (by your definition of "full" tree)

    recursion ends when u have exactly N nodes.