Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit b9d49df

Browse files
committed
222 finish
1 parent 9d43bca commit b9d49df

File tree

4 files changed

+76
-56
lines changed

4 files changed

+76
-56
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# [222. Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/)
2+
3+
Given a complete binary tree, count the number of nodes.
4+
5+
Note:
6+
7+
Definition of a complete binary tree from [Wikipedia](http://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees):
8+
9+
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
10+
11+
Example:
12+
13+
```text
14+
Input:
15+
1
16+
/ \
17+
2 3
18+
/ \ /
19+
4 5 6
20+
21+
Output: 6
22+
```
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
package problem0222
22

3-
type TreeNode struct {
4-
Val int
5-
Left *TreeNode
6-
Right *TreeNode
7-
}
3+
import (
4+
"github.com/aQuaYi/LeetCode-in-Go/kit"
5+
)
86

9-
func countNodes(root *TreeNode) int {
10-
if root == nil {
11-
return 0
12-
}
7+
// TreeNode is predefined in kit
8+
type TreeNode = kit.TreeNode
139

10+
func countNodes(root *TreeNode) int {
1411
count := 0
1512
traverse(root, &count)
1613
return count
1714
}
1815

19-
func traverse(node *TreeNode, count *int) {
20-
if node == nil {
16+
func traverse(n *TreeNode, count *int) {
17+
if n == nil {
2118
return
2219
}
2320

2421
*count++
2522

26-
traverse(node.Left, count)
27-
traverse(node.Right, count)
23+
traverse(n.Left, count)
24+
traverse(n.Right, count)
2825
}

Algorithms/0222.count-complete-tree-nodes/count-complete-tree-nodes_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var questions = []struct {
1010
pre, in []int
1111
count int
1212
}{
13+
1314
{
1415
[]int{},
1516
[]int{},

0 commit comments

Comments
 (0)