95. Unique Binary Search Trees II
# Medium
Last updated
Was this helpful?
# Medium
Last updated
Was this helpful?
不是很懂为什么在helper
函数的开头要定义一个空的bst
数组? 有点难做,需要再练习来加强。
subTree(start, end)
是在区间[start, end]
中找出所有的解法,那么就是移动root
,root = [start: end]
,返回值是一个叫bst
的list
,存放所有解法的TreeNode(root)
树结构。
由于每次递归时,bst
都是新的,因此helper函数的开头新建了一个空bst
;
root
左边有n
个解法,root
右边有m
个解法,那么一共就有n*m
个解法,所以一定要用两层嵌套的for循环得到新的bst
;
helper函数中的终止条件要注意,当start>end
时才终止,这时候一定要加上NULL
,不然那么多NULL
哪里加呢。这一点是最难想清楚的。
Time complexity: 很难算,空间复杂度是一样的。