-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path222-CountCompleteTreeNodes.go
More file actions
127 lines (113 loc) · 3.1 KB
/
222-CountCompleteTreeNodes.go
File metadata and controls
127 lines (113 loc) · 3.1 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
// 222. Count Complete Tree Nodes
// Given the root of a complete binary tree, return the number of the nodes in the tree.
// According to Wikipedia, every level, except possibly the last,
// is completely filled in a complete binary tree,
// 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.
// Design an algorithm that runs in less than O(n) time complexity.
// Example 1:
// 1
// / \
// 2 3
// / \ /
// 4 5 6
// <img src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" />
// Input: root = [1,2,3,4,5,6]
// Output: 6
// Example 2:
// Input: root = []
// Output: 0
// Example 3:
// Input: root = [1]
// Output: 1
// Constraints:
// The number of nodes in the tree is in the range [0, 5 * 10^4].
// 0 <= Node.val <= 5 * 10^4
// The tree is guaranteed to be complete.
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func countNodes(root *TreeNode) int {
res := 1
if root == nil {
return 0
}
var dfs func(root *TreeNode)
dfs = func(root *TreeNode) {
if root == nil {
return
}
if root.Left != nil {
res++
dfs(root.Left)
}
if root.Right != nil {
res++
dfs(root.Right)
}
}
dfs(root)
return res
}
func countNodes1(root *TreeNode) int {
// Define var to track count
res := 0
var dfs func (root *TreeNode, currSum *int)
dfs = func (root *TreeNode, currSum *int) {
// Cut condition. If node is nil, return
if root == nil {
return
}
*currSum++ // As node is not nil, count it
if root.Left != nil { // If there's left subtree to check, go and count nodes there
dfs(root.Left, currSum)
}
if root.Right != nil { // If there's right subtree to check, go and count nodes there
dfs(root.Right, currSum)
}
}
// Pass it as reference
dfs(root, &res)
return res
}
func main() {
// Example 1:
// 1
// / \
// 2 3
// / \ /
// 4 5 6
// <img src="https://assets.leetcode.com/uploads/2021/01/14/complete.jpg" />
// Input: root = [1,2,3,4,5,6]
// Output: 6
tree1 := &TreeNode {
1,
&TreeNode { 2, &TreeNode { 4, nil, nil}, &TreeNode { 5, nil, nil}, },
&TreeNode { 3, &TreeNode { 6, nil, nil}, nil},
}
fmt.Println(countNodes(tree1)) // 6
// Example 2:
// Input: root = []
// Output: 0
fmt.Println(countNodes(nil)) // 0
// Example 3:
// Input: root = [1]
// Output: 1
fmt.Println(countNodes(&TreeNode { 1, nil, nil })) // 1
fmt.Println(countNodes1(tree1)) // 6
fmt.Println(countNodes1(nil)) // 0
fmt.Println(countNodes1(&TreeNode { 1, nil, nil })) // 1
}