Skip to content

Commit 328b1fc

Browse files
committed
feat: add solutions to lc problem: No.0222. Count Complete Tree Nodes
1 parent 804587d commit 328b1fc

File tree

7 files changed

+484
-47
lines changed

7 files changed

+484
-47
lines changed

solution/0200-0299/0222.Count Complete Tree Nodes/README.md

+174-1
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,188 @@
5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

6161
```python
62-
62+
# Definition for a binary tree node.
63+
# class TreeNode:
64+
# def __init__(self, val=0, left=None, right=None):
65+
# self.val = val
66+
# self.left = left
67+
# self.right = right
68+
class Solution:
69+
def countNodes(self, root: TreeNode) -> int:
70+
def depth(root):
71+
res = 0
72+
while root:
73+
res += 1
74+
root = root.left
75+
return res
76+
77+
if root is None:
78+
return 0
79+
left_depth = depth(root.left)
80+
right_depth = depth(root.right)
81+
if left_depth > right_depth:
82+
return (1 << right_depth) + self.countNodes(root.left)
83+
return (1 << left_depth) + self.countNodes(root.right)
6384
```
6485

6586
### **Java**
6687

6788
<!-- 这里可写当前语言的特殊实现逻辑 -->
6889

6990
```java
91+
/**
92+
* Definition for a binary tree node.
93+
* public class TreeNode {
94+
* int val;
95+
* TreeNode left;
96+
* TreeNode right;
97+
* TreeNode() {}
98+
* TreeNode(int val) { this.val = val; }
99+
* TreeNode(int val, TreeNode left, TreeNode right) {
100+
* this.val = val;
101+
* this.left = left;
102+
* this.right = right;
103+
* }
104+
* }
105+
*/
106+
class Solution {
107+
public int countNodes(TreeNode root) {
108+
if (root == null) {
109+
return 0;
110+
}
111+
int leftDepth = depth(root.left);
112+
int rightDepth = depth(root.right);
113+
if (leftDepth > rightDepth) {
114+
return (1 << rightDepth) + countNodes(root.left);
115+
}
116+
return (1 << leftDepth) + countNodes(root.right);
117+
}
118+
119+
private int depth(TreeNode root) {
120+
int res = 0;
121+
while (root != null) {
122+
++res;
123+
root = root.left;
124+
}
125+
return res;
126+
}
127+
}
128+
```
129+
130+
### **C++**
131+
132+
```cpp
133+
/**
134+
* Definition for a binary tree node.
135+
* struct TreeNode {
136+
* int val;
137+
* TreeNode *left;
138+
* TreeNode *right;
139+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
140+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
141+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
142+
* };
143+
*/
144+
class Solution {
145+
public:
146+
int countNodes(TreeNode* root) {
147+
if (!root) {
148+
return 0;
149+
}
150+
int leftDepth = depth(root->left);
151+
int rightDepth = depth(root->right);
152+
if (leftDepth > rightDepth) {
153+
return (1 << rightDepth) + countNodes(root->left);
154+
}
155+
return (1 << leftDepth) + countNodes(root->right);
156+
}
157+
158+
private:
159+
int depth(TreeNode* root) {
160+
int res = 0;
161+
while (root) {
162+
++res;
163+
root = root->left;
164+
}
165+
return res;
166+
}
167+
};
168+
```
169+
170+
### **Go**
171+
172+
```go
173+
/**
174+
* Definition for a binary tree node.
175+
* type TreeNode struct {
176+
* Val int
177+
* Left *TreeNode
178+
* Right *TreeNode
179+
* }
180+
*/
181+
func countNodes(root *TreeNode) int {
182+
if root == nil {
183+
return 0
184+
}
185+
leftDepth := depth(root.Left)
186+
rightDepth := depth(root.Right)
187+
if leftDepth > rightDepth {
188+
return (1 << rightDepth) + countNodes(root.Left)
189+
}
190+
return (1 << leftDepth) + countNodes(root.Right)
191+
}
192+
193+
func depth(root *TreeNode) int {
194+
res := 0
195+
for root != nil {
196+
res++
197+
root = root.Left
198+
}
199+
return res
200+
}
201+
```
70202

203+
### **C#**
204+
205+
```cs
206+
/**
207+
* Definition for a binary tree node.
208+
* public class TreeNode {
209+
* public int val;
210+
* public TreeNode left;
211+
* public TreeNode right;
212+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
213+
* this.val = val;
214+
* this.left = left;
215+
* this.right = right;
216+
* }
217+
* }
218+
*/
219+
public class Solution {
220+
public int CountNodes(TreeNode root) {
221+
if (root == null)
222+
{
223+
return 0;
224+
}
225+
int leftDepth = depth(root.left);
226+
int rightDepth = depth(root.right);
227+
if (leftDepth > rightDepth)
228+
{
229+
return (1 << rightDepth) + CountNodes(root.left);
230+
}
231+
return (1 << leftDepth) + CountNodes(root.right);
232+
}
233+
234+
private int depth(TreeNode root) {
235+
int res = 0;
236+
while (root != null)
237+
{
238+
++res;
239+
root = root.left;
240+
}
241+
return res;
242+
}
243+
}
71244
```
72245

73246
### **...**

solution/0200-0299/0222.Count Complete Tree Nodes/README_EN.md

+174-1
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,186 @@
4949
### **Python3**
5050

5151
```python
52-
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, val=0, left=None, right=None):
55+
# self.val = val
56+
# self.left = left
57+
# self.right = right
58+
class Solution:
59+
def countNodes(self, root: TreeNode) -> int:
60+
def depth(root):
61+
res = 0
62+
while root:
63+
res += 1
64+
root = root.left
65+
return res
66+
67+
if root is None:
68+
return 0
69+
left_depth = depth(root.left)
70+
right_depth = depth(root.right)
71+
if left_depth > right_depth:
72+
return (1 << right_depth) + self.countNodes(root.left)
73+
return (1 << left_depth) + self.countNodes(root.right)
5374
```
5475

5576
### **Java**
5677

5778
```java
79+
/**
80+
* Definition for a binary tree node.
81+
* public class TreeNode {
82+
* int val;
83+
* TreeNode left;
84+
* TreeNode right;
85+
* TreeNode() {}
86+
* TreeNode(int val) { this.val = val; }
87+
* TreeNode(int val, TreeNode left, TreeNode right) {
88+
* this.val = val;
89+
* this.left = left;
90+
* this.right = right;
91+
* }
92+
* }
93+
*/
94+
class Solution {
95+
public int countNodes(TreeNode root) {
96+
if (root == null) {
97+
return 0;
98+
}
99+
int leftDepth = depth(root.left);
100+
int rightDepth = depth(root.right);
101+
if (leftDepth > rightDepth) {
102+
return (1 << rightDepth) + countNodes(root.left);
103+
}
104+
return (1 << leftDepth) + countNodes(root.right);
105+
}
106+
107+
private int depth(TreeNode root) {
108+
int res = 0;
109+
while (root != null) {
110+
++res;
111+
root = root.left;
112+
}
113+
return res;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
/**
122+
* Definition for a binary tree node.
123+
* struct TreeNode {
124+
* int val;
125+
* TreeNode *left;
126+
* TreeNode *right;
127+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
128+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
129+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
130+
* };
131+
*/
132+
class Solution {
133+
public:
134+
int countNodes(TreeNode* root) {
135+
if (!root) {
136+
return 0;
137+
}
138+
int leftDepth = depth(root->left);
139+
int rightDepth = depth(root->right);
140+
if (leftDepth > rightDepth) {
141+
return (1 << rightDepth) + countNodes(root->left);
142+
}
143+
return (1 << leftDepth) + countNodes(root->right);
144+
}
145+
146+
private:
147+
int depth(TreeNode* root) {
148+
int res = 0;
149+
while (root) {
150+
++res;
151+
root = root->left;
152+
}
153+
return res;
154+
}
155+
};
156+
```
157+
158+
### **Go**
159+
160+
```go
161+
/**
162+
* Definition for a binary tree node.
163+
* type TreeNode struct {
164+
* Val int
165+
* Left *TreeNode
166+
* Right *TreeNode
167+
* }
168+
*/
169+
func countNodes(root *TreeNode) int {
170+
if root == nil {
171+
return 0
172+
}
173+
leftDepth := depth(root.Left)
174+
rightDepth := depth(root.Right)
175+
if leftDepth > rightDepth {
176+
return (1 << rightDepth) + countNodes(root.Left)
177+
}
178+
return (1 << leftDepth) + countNodes(root.Right)
179+
}
180+
181+
func depth(root *TreeNode) int {
182+
res := 0
183+
for root != nil {
184+
res++
185+
root = root.Left
186+
}
187+
return res
188+
}
189+
```
58190

191+
### **C#**
192+
193+
```cs
194+
/**
195+
* Definition for a binary tree node.
196+
* public class TreeNode {
197+
* public int val;
198+
* public TreeNode left;
199+
* public TreeNode right;
200+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
201+
* this.val = val;
202+
* this.left = left;
203+
* this.right = right;
204+
* }
205+
* }
206+
*/
207+
public class Solution {
208+
public int CountNodes(TreeNode root) {
209+
if (root == null)
210+
{
211+
return 0;
212+
}
213+
int leftDepth = depth(root.left);
214+
int rightDepth = depth(root.right);
215+
if (leftDepth > rightDepth)
216+
{
217+
return (1 << rightDepth) + CountNodes(root.left);
218+
}
219+
return (1 << leftDepth) + CountNodes(root.right);
220+
}
221+
222+
private int depth(TreeNode root) {
223+
int res = 0;
224+
while (root != null)
225+
{
226+
++res;
227+
root = root.left;
228+
}
229+
return res;
230+
}
231+
}
59232
```
60233

61234
### **...**

0 commit comments

Comments
 (0)