Skip to content

Commit 0dddae0

Browse files
committed
github.com/masx200/leetcode-test/count-complete-tree-nodes
1 parent 932a251 commit 0dddae0

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

count-complete-tree-nodes/export.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package index
2+
import serialize_and_deserialize_binary_tree "github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree"
3+
4+
func CountNodes(root *TreeNode) int {
5+
return countNodes(root)
6+
}
7+
type TreeNode = serialize_and_deserialize_binary_tree.TreeNode

count-complete-tree-nodes/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/masx200/leetcode-test/count-complete-tree-nodes
2+
3+
go 1.19
4+
5+
require github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220906144335-3503ebe76485

count-complete-tree-nodes/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
2+
github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2 h1:33VEOTS2x7yTFGGRWFVfNVyfgHgMKp3J44Cm40gv0WU=
3+
github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220825072430-380d3e0e30a2/go.mod h1:WERZIbs/GoCIGQMt6FbOuDZBOkCi2oQlWFF88WfmKYI=
4+
github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220906144335-3503ebe76485 h1:punpL7/3LN8fDFG5aRBnlGxyrfwWkngw9exLPtW7lzM=
5+
github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree v0.0.0-20220906144335-3503ebe76485/go.mod h1:WERZIbs/GoCIGQMt6FbOuDZBOkCi2oQlWFF88WfmKYI=
6+
gotest.tools/v3 v3.3.0 h1:MfDY1b1/0xN1CyMlQDac0ziEy9zJQd9CXBRRDHw2jJo=

count-complete-tree-nodes/index.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package index
2+
3+
import "math"
4+
5+
func countNodes(root *TreeNode) int {
6+
return countNodesDepth(root, -1, -1)
7+
}
8+
9+
func countNodesDepth(root *TreeNode, leftDepth int, rightDepth int) int {
10+
if root == nil {
11+
return 0
12+
}
13+
14+
var left = 0
15+
16+
var right = 0
17+
18+
var curNode = root
19+
20+
if leftDepth > 0 {
21+
22+
left = leftDepth
23+
} else {
24+
25+
for curNode != nil {
26+
27+
left++
28+
curNode = curNode.Left
29+
}
30+
31+
}
32+
curNode = root
33+
34+
if rightDepth > 0 {
35+
36+
right = rightDepth
37+
} else {
38+
39+
for curNode != nil {
40+
41+
right++
42+
curNode = curNode.Right
43+
}
44+
45+
}
46+
47+
if left == right {
48+
49+
return int(math.Pow(2, float64(left))) - 1
50+
}
51+
52+
return (1 + countNodesDepth(root.Left, left-1, -1) + countNodesDepth(root.Right, -1, right-1))
53+
54+
}

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ use (
1616
./serialize-and-deserialize-binary-tree
1717
./transform-to-chessboard
1818
./utils
19+
./count-complete-tree-nodes
1920
)

rearrange-spaces-between-words/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function reorderSpaces(text: string): string {
77
text,
88
//@ts-ignore
99
(a, v) => a + Number(v === " "),
10-
0
10+
0,
1111
) as number;
1212

1313
if (words.length <= 1) return words.join("") + " ".repeat(spaces);

0 commit comments

Comments
 (0)