How many permutations of a given array result in BST's of height 2?

后端 未结 3 1496
旧巷少年郎
旧巷少年郎 2021-02-06 11:48

A BST is generated (by successive insertion of nodes) from each permutation of keys from the set {1,2,3,4,5,6,7}. How many permutations determine trees of height two?

I

3条回答
  •  日久生厌
    2021-02-06 12:03

    Consider how the tree would be height 2?

    -It needs to have 4 as root, 2 as the left child, 6 right child, etc.

    How come 4 is the root?

    -It needs to be the first inserted. So we have one number now, 6 still can move around in the permutation.

    And?

    -After the first insert there are still 6 places left, 3 for the left and 3 for the right subtrees. That's 6 choose 3 = 20 choices.

    Now what?

    -For the left and right subtrees, their roots need to be inserted first, then the children's order does not affect the tree - 2, 1, 3 and 2, 3, 1 gives the same tree. That's 2 for each subtree, and 2 * 2 = 4 for the left and right subtrees.

    So?

    In conclusion: C(6, 3) * 2 * 2 = 20 * 2 * 2 = 80.

提交回复
热议问题