Skip to content

Commit 564a092

Browse files
committed
feat: update solutions to lc problems: No.0222,0270
* No.0222.Count Complete Tree Nodes * No.0270.Closest Binary Search Tree Value
1 parent c54388e commit 564a092

File tree

15 files changed

+327
-287
lines changed

15 files changed

+327
-287
lines changed

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

+79-51
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
朴素解法是直接遍历整棵完全二叉树,统计结点个数。时间复杂度 O(n)。
55+
56+
对于此题,我们还可以利用完全二叉树的特点,设计一个更快的算法。
57+
58+
完全二叉树的特点:叶子结点只能出现在最下层和次下层,且最下层的叶子结点集中在树的左部。需要注意的是,满二叉树肯定是完全二叉树,而完全二叉树不一定是满二叉树。
59+
60+
若满二叉树的层数为 h,则总结点数为 `2^h - 1`
61+
62+
我们可以先对 root 的左右子树进行高度统计,分别记为 left, right。
63+
64+
1.`left == right`,说明左子树是一颗满二叉树。所以左子树的结点总数为 `2^left - 1`,加上 root 结点,就是 `2^left`,然后递归统计右子树即可。
65+
1.`left > right`,说明右子树是一个满二叉树。所以右子树的结点总数为 `2^right - 1`,加上 root 结点,就是 `2^right`,然后递归统计左子树即可。
66+
67+
时间复杂度 O(log²n)。
68+
5469
<!-- tabs:start -->
5570

5671
### **Python3**
@@ -75,11 +90,10 @@ class Solution:
7590

7691
if root is None:
7792
return 0
78-
left_depth = depth(root.left)
79-
right_depth = depth(root.right)
80-
if left_depth > right_depth:
81-
return (1 << right_depth) + self.countNodes(root.left)
82-
return (1 << left_depth) + self.countNodes(root.right)
93+
left, right = depth(root.left), depth(root.right)
94+
if left == right:
95+
return (1 << left) + self.countNodes(root.right)
96+
return (1 << right) + self.countNodes(root.left)
8397
```
8498

8599
### **Java**
@@ -107,20 +121,17 @@ class Solution {
107121
if (root == null) {
108122
return 0;
109123
}
110-
int leftDepth = depth(root.left);
111-
int rightDepth = depth(root.right);
112-
if (leftDepth > rightDepth) {
113-
return (1 << rightDepth) + countNodes(root.left);
124+
int left = depth(root.left);
125+
int right = depth(root.right);
126+
if (left == right) {
127+
return (1 << left) + countNodes(root.right);
114128
}
115-
return (1 << leftDepth) + countNodes(root.right);
129+
return (1 << right) + countNodes(root.left);
116130
}
117131

118132
private int depth(TreeNode root) {
119133
int res = 0;
120-
while (root != null) {
121-
++res;
122-
root = root.left;
123-
}
134+
for (; root != null; root = root.left, ++res);
124135
return res;
125136
}
126137
}
@@ -143,24 +154,16 @@ class Solution {
143154
class Solution {
144155
public:
145156
int countNodes(TreeNode* root) {
146-
if (!root) {
147-
return 0;
148-
}
149-
int leftDepth = depth(root->left);
150-
int rightDepth = depth(root->right);
151-
if (leftDepth > rightDepth) {
152-
return (1 << rightDepth) + countNodes(root->left);
153-
}
154-
return (1 << leftDepth) + countNodes(root->right);
157+
if (!root) return 0;
158+
int left = depth(root->left);
159+
int right = depth(root->right);
160+
if (left == right) return (1 << left) + countNodes(root->right);
161+
return (1 << right) + countNodes(root->left);
155162
}
156163

157-
private:
158164
int depth(TreeNode* root) {
159165
int res = 0;
160-
while (root) {
161-
++res;
162-
root = root->left;
163-
}
166+
for (; root != nullptr; ++res, root = root->left);
164167
return res;
165168
}
166169
};
@@ -181,24 +184,53 @@ func countNodes(root *TreeNode) int {
181184
if root == nil {
182185
return 0
183186
}
184-
leftDepth := depth(root.Left)
185-
rightDepth := depth(root.Right)
186-
if leftDepth > rightDepth {
187-
return (1 << rightDepth) + countNodes(root.Left)
187+
depth := func(root *TreeNode) int {
188+
res := 0
189+
for root != nil {
190+
res++
191+
root = root.Left
192+
}
193+
return res
188194
}
189-
return (1 << leftDepth) + countNodes(root.Right)
190-
}
191-
192-
func depth(root *TreeNode) int {
193-
res := 0
194-
for root != nil {
195-
res++
196-
root = root.Left
195+
left, right := depth(root.Left), depth(root.Right)
196+
if left == right {
197+
return (1 << left) + countNodes(root.Right)
197198
}
198-
return res
199+
return (1 << right) + countNodes(root.Left)
199200
}
200201
```
201202

203+
### **JavaScript**
204+
205+
```js
206+
/**
207+
* Definition for a binary tree node.
208+
* function TreeNode(val, left, right) {
209+
* this.val = (val===undefined ? 0 : val)
210+
* this.left = (left===undefined ? null : left)
211+
* this.right = (right===undefined ? null : right)
212+
* }
213+
*/
214+
/**
215+
* @param {TreeNode} root
216+
* @return {number}
217+
*/
218+
var countNodes = function (root) {
219+
if (!root) return 0;
220+
let depth = function (root) {
221+
let res = 0;
222+
for (; root != null; ++res, root = root.left);
223+
return res;
224+
};
225+
const left = depth(root.left);
226+
const right = depth(root.right);
227+
if (left == right) {
228+
return (1 << left) + countNodes(root.right);
229+
}
230+
return (1 << right) + countNodes(root.left);
231+
};
232+
```
233+
202234
### **C#**
203235

204236
```cs
@@ -221,22 +253,18 @@ public class Solution {
221253
{
222254
return 0;
223255
}
224-
int leftDepth = depth(root.left);
225-
int rightDepth = depth(root.right);
226-
if (leftDepth > rightDepth)
256+
int left = depth(root.left);
257+
int right = depth(root.right);
258+
if (left == right)
227259
{
228-
return (1 << rightDepth) + CountNodes(root.left);
260+
return (1 << left) + CountNodes(root.right);
229261
}
230-
return (1 << leftDepth) + CountNodes(root.right);
262+
return (1 << right) + CountNodes(root.left);
231263
}
232264

233265
private int depth(TreeNode root) {
234266
int res = 0;
235-
while (root != null)
236-
{
237-
++res;
238-
root = root.left;
239-
}
267+
for (; root != null; ++res, root = root.left);
240268
return res;
241269
}
242270
}

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

+64-51
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,10 @@ class Solution:
6666

6767
if root is None:
6868
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)
69+
left, right = depth(root.left), depth(root.right)
70+
if left == right:
71+
return (1 << left) + self.countNodes(root.right)
72+
return (1 << right) + self.countNodes(root.left)
7473
```
7574

7675
### **Java**
@@ -96,20 +95,17 @@ class Solution {
9695
if (root == null) {
9796
return 0;
9897
}
99-
int leftDepth = depth(root.left);
100-
int rightDepth = depth(root.right);
101-
if (leftDepth > rightDepth) {
102-
return (1 << rightDepth) + countNodes(root.left);
98+
int left = depth(root.left);
99+
int right = depth(root.right);
100+
if (left == right) {
101+
return (1 << left) + countNodes(root.right);
103102
}
104-
return (1 << leftDepth) + countNodes(root.right);
103+
return (1 << right) + countNodes(root.left);
105104
}
106105

107106
private int depth(TreeNode root) {
108107
int res = 0;
109-
while (root != null) {
110-
++res;
111-
root = root.left;
112-
}
108+
for (; root != null; root = root.left, ++res);
113109
return res;
114110
}
115111
}
@@ -132,24 +128,16 @@ class Solution {
132128
class Solution {
133129
public:
134130
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);
131+
if (!root) return 0;
132+
int left = depth(root->left);
133+
int right = depth(root->right);
134+
if (left == right) return (1 << left) + countNodes(root->right);
135+
return (1 << right) + countNodes(root->left);
144136
}
145137

146-
private:
147138
int depth(TreeNode* root) {
148139
int res = 0;
149-
while (root) {
150-
++res;
151-
root = root->left;
152-
}
140+
for (; root != nullptr; ++res, root = root->left);
153141
return res;
154142
}
155143
};
@@ -170,24 +158,53 @@ func countNodes(root *TreeNode) int {
170158
if root == nil {
171159
return 0
172160
}
173-
leftDepth := depth(root.Left)
174-
rightDepth := depth(root.Right)
175-
if leftDepth > rightDepth {
176-
return (1 << rightDepth) + countNodes(root.Left)
161+
depth := func(root *TreeNode) int {
162+
res := 0
163+
for root != nil {
164+
res++
165+
root = root.Left
166+
}
167+
return res
177168
}
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
169+
left, right := depth(root.Left), depth(root.Right)
170+
if left == right {
171+
return (1 << left) + countNodes(root.Right)
186172
}
187-
return res
173+
return (1 << right) + countNodes(root.Left)
188174
}
189175
```
190176

177+
### **JavaScript**
178+
179+
```js
180+
/**
181+
* Definition for a binary tree node.
182+
* function TreeNode(val, left, right) {
183+
* this.val = (val===undefined ? 0 : val)
184+
* this.left = (left===undefined ? null : left)
185+
* this.right = (right===undefined ? null : right)
186+
* }
187+
*/
188+
/**
189+
* @param {TreeNode} root
190+
* @return {number}
191+
*/
192+
var countNodes = function (root) {
193+
if (!root) return 0;
194+
let depth = function (root) {
195+
let res = 0;
196+
for (; root != null; ++res, root = root.left);
197+
return res;
198+
};
199+
const left = depth(root.left);
200+
const right = depth(root.right);
201+
if (left == right) {
202+
return (1 << left) + countNodes(root.right);
203+
}
204+
return (1 << right) + countNodes(root.left);
205+
};
206+
```
207+
191208
### **C#**
192209

193210
```cs
@@ -210,22 +227,18 @@ public class Solution {
210227
{
211228
return 0;
212229
}
213-
int leftDepth = depth(root.left);
214-
int rightDepth = depth(root.right);
215-
if (leftDepth > rightDepth)
230+
int left = depth(root.left);
231+
int right = depth(root.right);
232+
if (left == right)
216233
{
217-
return (1 << rightDepth) + CountNodes(root.left);
234+
return (1 << left) + CountNodes(root.right);
218235
}
219-
return (1 << leftDepth) + CountNodes(root.right);
236+
return (1 << right) + CountNodes(root.left);
220237
}
221238

222239
private int depth(TreeNode root) {
223240
int res = 0;
224-
while (root != null)
225-
{
226-
++res;
227-
root = root.left;
228-
}
241+
for (; root != null; ++res, root = root.left);
229242
return res;
230243
}
231244
}

solution/0200-0299/0222.Count Complete Tree Nodes/Solution.cpp

+6-14
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,16 @@
1212
class Solution {
1313
public:
1414
int countNodes(TreeNode* root) {
15-
if (!root) {
16-
return 0;
17-
}
18-
int leftDepth = depth(root->left);
19-
int rightDepth = depth(root->right);
20-
if (leftDepth > rightDepth) {
21-
return (1 << rightDepth) + countNodes(root->left);
22-
}
23-
return (1 << leftDepth) + countNodes(root->right);
15+
if (!root) return 0;
16+
int left = depth(root->left);
17+
int right = depth(root->right);
18+
if (left == right) return (1 << left) + countNodes(root->right);
19+
return (1 << right) + countNodes(root->left);
2420
}
2521

26-
private:
2722
int depth(TreeNode* root) {
2823
int res = 0;
29-
while (root) {
30-
++res;
31-
root = root->left;
32-
}
24+
for (; root != nullptr; ++res, root = root->left);
3325
return res;
3426
}
3527
};

0 commit comments

Comments
 (0)