Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e55d3b

Browse files
author
侯利朋
committed
.\894.all-possible-full-binary-trees.go
1 parent fac9216 commit 3e55d3b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

894.all-possible-full-binary-trees.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @lc app=leetcode id=894 lang=golang
3+
*
4+
* [894] All Possible Full Binary Trees
5+
*/
6+
/**
7+
* Definition for a binary tree node.
8+
* type TreeNode struct {
9+
* Val int
10+
* Left *TreeNode
11+
* Right *TreeNode
12+
* }
13+
*/
14+
// 432 ms, faster than 70.00%
15+
// 满二叉树的总结点个数规律 , 奇数节点
16+
func allPossibleFBT(N int) []*TreeNode {
17+
// map 加速
18+
m := make(map[int][]*TreeNode)
19+
return FBT(N, m)
20+
}
21+
22+
func FBT(N int, m map[int][]*TreeNode) []*TreeNode {
23+
if v, ok := m[N]; ok {
24+
return v
25+
}
26+
rtn := []*TreeNode{}
27+
if N == 1 {
28+
rtn = append(rtn, &TreeNode{
29+
Val: 0,
30+
Left: nil,
31+
Right: nil,
32+
})
33+
} else if N&1 != 0 {
34+
for x := 0; x < N; x++ {
35+
y := N - 1 - x
36+
tree := &TreeNode{}
37+
tree.Val = 0
38+
for _, ltree := range FBT(x, m) {
39+
for _, rtree := range FBT(y, m) {
40+
rtn = append(rtn, &TreeNode{
41+
Val: 0,
42+
Left: ltree,
43+
Right: rtree,
44+
})
45+
}
46+
}
47+
}
48+
}
49+
m[N] = rtn
50+
return rtn
51+
}

0 commit comments

Comments
 (0)