여기서 제시 : http://cslibrary.stanford.edu/110/BinaryTrees.html#java구조적으로 다른 모든 이진 트리의 수를 계산하는 데 시간이 얼마나 걸릴까요? 상기 방법을 사용
12. countTrees() Solution (Java) /** For the key values 1...numKeys, how many structurally unique binary search trees are possible that store those keys? Strategy: consider that each value could be the root. Recursively find the size of the left and right subtrees. */ public static int countTrees(int numKeys) { if (numKeys <=1) { return(1); } else { // there will be one value at the root, with whatever remains // on the left and right each forming their own subtrees. // Iterate through all the values that could be the root... int sum = 0; int left, right, root; for (root=1; root<=numKeys; root++) { left = countTrees(root-1); right = countTrees(numKeys - root); // number of possible trees with this root == left*right sum += left*right; } return(sum); } }
I 그것이 해당 될 수있는 감이 (N-1) (N-2) ... (1), 즉 N!
memoizer를 사용하는 경우 복잡도 O (n)입니까?