-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp894.go
More file actions
54 lines (42 loc) · 816 Bytes
/
Copy pathp894.go
File metadata and controls
54 lines (42 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import "fmt"
//TreeNode ...
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var forest = make(map[int][]*TreeNode)
func allPossibleFBT(N int) []*TreeNode {
if N%2 == 0 {
return []*TreeNode{}
}
if N == 1 {
return []*TreeNode{&TreeNode{0, nil, nil}}
}
if forest[N] == nil {
trees := make([]*TreeNode, 0)
for i := 1; i < N; i++ {
j := N - 1 - i
for _, left := range allPossibleFBT(i) {
for _, right := range allPossibleFBT(j) {
root := &TreeNode{0, left, right}
trees = append(trees, root)
}
}
}
forest[N] = trees
}
return forest[N]
}
func main() {
fmt.Println(len(allPossibleFBT(17)))
}