The possible number of binary search trees that can be created with N keys is given by the Nth catalan number. Why?

前端 未结 3 893
感情败类
感情败类 2020-12-02 17:35

This has been bothering me for a while. I know that given N keys to arrange in the form of a binary search tree, the possible number of trees that can be created correspond

3条回答
  •  广开言路
    2020-12-02 18:02

    Well here is the recursive solution to count the trees...

    int countTrees(int numkeys){
    
    if(numkeys > 1){
        int i =1;
        int sum=0;
    
        for(i = 1; i <= numkeys; i++){
    
            int lcount = countTrees(i-1);
            int rcount = countTrees(numkeys-i);
            sum += lcount*rcount;
        }
        return(sum);
    }else
        return(1);
    }
    

提交回复
热议问题