From 82c8d39e5595547115f6a8d8b5d6d751d83395f3 Mon Sep 17 00:00:00 2001 From: fengzw Date: Sat, 8 May 2021 19:52:24 +0800 Subject: [PATCH] add: original solution for Challenge3_Count_of_Structurally_Unique_Binary_Search_Trees --- .../solution.go | 81 ++++++++++++------- 1 file changed, 54 insertions(+), 27 deletions(-) diff --git a/Pattern10 - Subsets/Challenge3-Count_of_Structurally_Unique_Binary_Search_Trees/solution.go b/Pattern10 - Subsets/Challenge3-Count_of_Structurally_Unique_Binary_Search_Trees/solution.go index b3104da..2a0c206 100644 --- a/Pattern10 - Subsets/Challenge3-Count_of_Structurally_Unique_Binary_Search_Trees/solution.go +++ b/Pattern10 - Subsets/Challenge3-Count_of_Structurally_Unique_Binary_Search_Trees/solution.go @@ -4,38 +4,65 @@ package Challenge3_Count_of_Structurally_Unique_Binary_Search_Trees 已经在challenge2中实现 ref: https://leetcode-cn.com/problems/unique-binary-search-trees/ */ + func numTrees(n int) int { - var bfs func(int, int) int - var m = make(map[Range]int) - bfs = func(start, end int) int { - var res = 0 - if start == end { - return 1 - } - if v, ok := m[Range{start, end}]; ok { + var m = make(map[int]int) + + var countTrees func(int) int + countTrees = func(n int) int { + if v,ok:=m[n];ok{ return v } - for i := start; i <= end; i++ { - if i == start { - res += bfs(i+1, end) - continue - } - if i == end { - res += bfs(start, i-1) - continue - } - left := bfs(start, i-1) - right := bfs(i+1, end) - res += left * right + if n<=1 { + return 1 + } + + count := 0 + for i:=1;i<=n;i++{ + leftTrees := countTrees(i-1) + rightTrees := countTrees(n-i) + count += leftTrees*rightTrees } - m[Range{start, end}] = res - return res + + m[n]=count + return count } - return bfs(1, n) + return countTrees(n) } -type Range struct { - start int - end int -} +// func numTreesBackup(n int) int { +// var bfs func(int, int) int +// var m = make(map[Range]int) +// bfs = func(start, end int) int { +// var res = 0 +// if start == end { +// return 1 +// } +// if v, ok := m[Range{start, end}]; ok { +// return v +// } +// for i := start; i <= end; i++ { +// if i == start { +// res += bfs(i+1, end) +// continue +// } +// if i == end { +// res += bfs(start, i-1) +// continue +// } +// left := bfs(start, i-1) +// right := bfs(i+1, end) +// res += left * right +// } +// m[Range{start, end}] = res +// return res +// } + +// return bfs(1, n) +// } + +// type Range struct { +// start int +// end int +// }