Count number of unique node sequences producing the same BST

橙三吉。 提交于 2019-12-14 03:44:52

问题


The problem:

Given a particular sequence of up to 50 integers, which represent nodes of a certain binary search tree (BST), how many permutations of this sequence are there, which will also give rise to exactly the same BST? Include the original sequence in your total count as 1 sequence.

For instance, answer = 6 for such a sequence [5,2,1,9,8]: [5,2,1,9,8] (itself, the original sequence given), [5,9,8,2,1], [5,2,9,1,8], [5,9,2,1,8], [5,2,9,8,1], [5,9,2,8,1]


回答1:


Suppose you have your example sequence [5,2,1,9,8]. The first node will become the root of the binary tree, so we know the first node must be 5.

From then on, all the nodes smaller than 5 will go to the left child, and all the nodes greater than 5 will go to the right child.

So you can solve this recursively, count the number of ways of making the left subtree (consisting of [2,1]) multiply by the number of ways of making the right subtree (consisting of [9,8]) and multiply by the number of ways of ordering the arrival of integers on opposite sides.

Sample code

def nCr(n,r):
   """Return number of ways of choosing r objects from n"""

   top, bottom = 1, 1
   for i in xrange(r):
      top *= n
      n = n - 1
      bottom *= r
      r = r- 1
   return top / bottom

def count_ways_to_make_bst(seq):
    if len(seq)<=1:
        return 1
    num_remaining = len(seq) - 1
    left = [r for r in seq[1:] if r>seq[0]]
    right = [r for r in seq[1:] if r<seq[0]]
    return count_ways_to_make_bst(left) * count_ways_to_make_bst(right) * nCr(num_remaining,len(left))



回答2:


Assume that the given node sequence is the order in which we added those elements into the BST.

We can easily see that if two nodes a and b belong to two different branches, so the order of adding a and b will not affect the final structure.

We only need to ensure that the parent need to be added first, before its children is added.

For the example [5,2,1,9,8]: 5 should always added first.

There will be only two choices: add 2 or add 9

  • If we add 2: 1 can be added in any order
  • similarly, If we add 9: 8 can be added anytime after that

So, now we have our algo while travelling from root -> leaf:

int numberOfWays (Node node) {
    if(node is leaf) return 1;
    int a = numberOfWays(node.left);
    int b = numberOfWays(node.right);
    int x = number of node in the left;
    int y = number of node in the right;
    //nCr is combination, so we can choose x places in total of x + y positions
    return nCr(x+y,x) * a * b;
}


来源:https://stackoverflow.com/questions/28305860/count-number-of-unique-node-sequences-producing-the-same-bst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!