Skip to content

Commit 4eff82c

Browse files
committed
leetcode
1 parent b52f399 commit 4eff82c

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
3+
4+
-* 222. Count Complete Tree Nodes *-
5+
6+
Given the root of a complete binary tree, return the number of the nodes in the tree.
7+
8+
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.
9+
10+
Design an algorithm that runs in less than O(n) time complexity.
11+
12+
13+
14+
Example 1:
15+
16+
17+
Input: root = [1,2,3,4,5,6]
18+
Output: 6
19+
Example 2:
20+
21+
Input: root = []
22+
Output: 0
23+
Example 3:
24+
25+
Input: root = [1]
26+
Output: 1
27+
28+
29+
Constraints:
30+
31+
The number of nodes in the tree is in the range [0, 5 * 104].
32+
0 <= Node.val <= 5 * 104
33+
The tree is guaranteed to be complete.
34+
35+
36+
*/
37+
38+
// Definition for a binary tree node.
39+
import 'dart:collection';
40+
import 'dart:math';
41+
42+
class TreeNode {
43+
int val;
44+
TreeNode? left;
45+
TreeNode? right;
46+
TreeNode([this.val = 0, this.left, this.right]);
47+
}
48+
49+
class A {
50+
int countNodes(TreeNode? root) {
51+
if (root == null) return 0;
52+
return countNodes(root.left) + countNodes(root.right) + 1;
53+
}
54+
}
55+
56+
class B {
57+
int countNodes(TreeNode? root) {
58+
if (root == null) {
59+
return 0;
60+
}
61+
int left = 0, right = 0;
62+
TreeNode? leftNode = root, rightNode = root;
63+
while (leftNode != null) {
64+
left++;
65+
leftNode = leftNode.left;
66+
}
67+
68+
while (rightNode != null) {
69+
right++;
70+
rightNode = rightNode.right;
71+
}
72+
73+
if (left == right) {
74+
return pow(2, right).toInt() - 1;
75+
}
76+
77+
return 1 + countNodes(root.left) + countNodes(root.right);
78+
}
79+
}
80+
81+
class C {
82+
int count = 0;
83+
void solve(
84+
TreeNode? root,
85+
) {
86+
if (root == null) return;
87+
88+
count++;
89+
if (root.left != null) solve(root.left);
90+
if (root.right != null) solve(root.right);
91+
}
92+
93+
int countNodes(TreeNode? root) {
94+
solve(root);
95+
return count;
96+
}
97+
}
98+
99+
class D {
100+
int countNodes(TreeNode? root) {
101+
int count = 0;
102+
if (root == null) return 0;
103+
104+
Queue<TreeNode?> q = Queue();
105+
q.add(root);
106+
107+
while (!q.isEmpty) {
108+
root = q.first;
109+
q.removeFirst();
110+
if (root == null) {
111+
if (!q.isEmpty) q.add(null);
112+
} else {
113+
count++;
114+
if (root.left != null) q.add(root.left);
115+
if (root.right != null) q.add(root.right);
116+
}
117+
}
118+
119+
return count;
120+
}
121+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
// Definition for a binary tree node.
4+
type TreeNode struct {
5+
Val int
6+
Left *TreeNode
7+
Right *TreeNode
8+
}
9+
10+
func countNodes(root *TreeNode) int {
11+
if root == nil {
12+
return 0
13+
}
14+
return 1 + countNodes(root.Left) + countNodes(root.Right)
15+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 🔥 Count Complete Tree Nodes 🔥 || 4 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Definition for a binary tree node
4+
5+
```dart
6+
class TreeNode {
7+
int val;
8+
TreeNode? left;
9+
TreeNode? right;
10+
TreeNode([this.val = 0, this.left, this.right]);
11+
}
12+
```
13+
14+
## Solution - 1
15+
16+
```dart
17+
class Solution {
18+
int countNodes(TreeNode? root) {
19+
if (root == null) return 0;
20+
return countNodes(root.left) + countNodes(root.right) + 1;
21+
}
22+
}
23+
```
24+
25+
## Solution - 2
26+
27+
```dart
28+
class Solution {
29+
int countNodes(TreeNode? root) {
30+
if (root == null) {
31+
return 0;
32+
}
33+
int left = 0, right = 0;
34+
TreeNode? leftNode = root, rightNode = root;
35+
while (leftNode != null) {
36+
left++;
37+
leftNode = leftNode.left;
38+
}
39+
40+
while (rightNode != null) {
41+
right++;
42+
rightNode = rightNode.right;
43+
}
44+
45+
if (left == right) {
46+
return pow(2, right).toInt() - 1;
47+
}
48+
49+
return 1 + countNodes(root.left) + countNodes(root.right);
50+
}
51+
}
52+
```
53+
54+
## Solution - 3
55+
56+
```dart
57+
class Solution {
58+
int count = 0;
59+
void solve(
60+
TreeNode? root,
61+
) {
62+
if (root == null) return;
63+
64+
count++;
65+
if (root.left != null) solve(root.left);
66+
if (root.right != null) solve(root.right);
67+
}
68+
69+
int countNodes(TreeNode? root) {
70+
solve(root);
71+
return count;
72+
}
73+
}
74+
```
75+
76+
## Solution - 4
77+
78+
```dart
79+
import 'dart:collection';
80+
81+
class Solution {
82+
int countNodes(TreeNode? root) {
83+
int count = 0;
84+
if (root == null) return 0;
85+
86+
Queue<TreeNode?> q = Queue();
87+
q.add(root);
88+
89+
while (!q.isEmpty) {
90+
root = q.first;
91+
q.removeFirst();
92+
if (root == null) {
93+
if (!q.isEmpty) q.add(null);
94+
} else {
95+
count++;
96+
if (root.left != null) q.add(root.left);
97+
if (root.right != null) q.add(root.right);
98+
}
99+
}
100+
101+
return count;
102+
}
103+
}
104+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
126126
- [**345.** Reverse Vowels of a String](ReverseVowelsOfAString/reverse_vowels_of_a_string.dart)
127127
- [**151.** Reverse Words in a String](ReverseWordsInAString/reverse_words_in_a_string.dart)
128128
- [**947.** Most Stones Removed with Same Row or Column](MostStonesRemovedWithSameRowOrColumn/most_stones_removed_with_same_row_or_column.dart)
129+
- [**222.** Count Complete Tree Nodes](CountCompleteTreeNodes/count_complete_tree_nodes.dart)
129130

130131
## Reach me via
131132

0 commit comments

Comments
 (0)