How many ways can you insert a series of values into a BST to form a specific tree?

后端 未结 2 1719
陌清茗
陌清茗 2021-01-30 15:01

This earlier question asked how many ways there were to insert the values 1 - 7 into a binary search tree that would result in the following tree:

       4
              


        
2条回答
  •  日久生厌
    2021-01-30 15:35

    This is an interesting question. I implemented this idea in python and this recursion plus memorization has pretty good performance. The "seq" is the input list of unique integers

    def answer(seq):
        from math import factorial
        BStore = dict() # store binomsial coefficient
        Store = dict() # store the recusion step 
        def TreeGenerator(a,L): # for a given number a and a list L, this functions returns the left tree (a list) and the right tree (a list)
            LT = []
            RT = []
            for i in L:
                if i

提交回复
热议问题