Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dac50d6

Browse files
feat: add rust solution to lc problem: No.0222
No.0222.Count Complete Tree Nodes
1 parent 7e5479a commit dac50d6

File tree

6 files changed

+96
-5
lines changed

6 files changed

+96
-5
lines changed

‎index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,4 @@
123123
<script src="//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/index.min.js"></script>
124124
</body>
125125

126-
</html>
126+
</html>

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,38 @@ public class Solution {
270270
}
271271
```
272272

273+
### **Rust**
274+
275+
```rust
276+
use std::cell::RefCell;
277+
use std::rc::Rc;
278+
279+
impl Solution {
280+
pub fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
281+
if let Some(node) = root {
282+
let node = node.borrow();
283+
let left = Self::depth(&node.left);
284+
let right = Self::depth(&node.right);
285+
if left == right {
286+
Self::count_nodes(node.right.clone()) + (1 << left)
287+
} else {
288+
Self::count_nodes(node.left.clone()) + (1 << right)
289+
}
290+
} else {
291+
0
292+
}
293+
}
294+
295+
fn depth(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
296+
if let Some(node) = root {
297+
Self::depth(&node.borrow().left) + 1
298+
} else {
299+
0
300+
}
301+
}
302+
}
303+
```
304+
273305
### **...**
274306

275307
```

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,38 @@ public class Solution {
244244
}
245245
```
246246

247+
### **Rust**
248+
249+
```rust
250+
use std::cell::RefCell;
251+
use std::rc::Rc;
252+
253+
impl Solution {
254+
pub fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
255+
if let Some(node) = root {
256+
let node = node.borrow();
257+
let left = Self::depth(&node.left);
258+
let right = Self::depth(&node.right);
259+
if left == right {
260+
Self::count_nodes(node.right.clone()) + (1 << left)
261+
} else {
262+
Self::count_nodes(node.left.clone()) + (1 << right)
263+
}
264+
} else {
265+
0
266+
}
267+
}
268+
269+
fn depth(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
270+
if let Some(node) = root {
271+
Self::depth(&node.borrow().left) + 1
272+
} else {
273+
0
274+
}
275+
}
276+
}
277+
```
278+
247279
### **...**
248280

249281
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::cell::RefCell;
2+
use std::rc::Rc;
3+
4+
impl Solution {
5+
pub fn count_nodes(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
6+
if let Some(node) = root {
7+
let node = node.borrow();
8+
let left = Self::depth(&node.left);
9+
let right = Self::depth(&node.right);
10+
if left == right {
11+
Self::count_nodes(node.right.clone()) + (1 << left)
12+
} else {
13+
Self::count_nodes(node.left.clone()) + (1 << right)
14+
}
15+
} else {
16+
0
17+
}
18+
}
19+
20+
fn depth(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
21+
if let Some(node) = root {
22+
Self::depth(&node.borrow().left) + 1
23+
} else {
24+
0
25+
}
26+
}
27+
}

‎solution/2000-2099/2000.Reverse Prefix of Word/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<pre><strong>输入:</strong>word = "<em><strong>abcd</strong></em>efd", ch = "d"
2222
<strong>输出:</strong>"<em><strong>dcba</strong></em>efd"
23-
<strong>解释:</strong>"d" 第一次出现在下标 3 。
23+
<strong>解释:</strong>"d" 第一次出现在下标 3 。
2424
反转从下标 0 到下标 3(含下标 3)的这段字符,结果字符串是 "dcbaefd" 。
2525
</pre>
2626

@@ -129,7 +129,7 @@ function reversePrefix(word: string, ch: string): string {
129129

130130
### **Rust**
131131

132-
```rs
132+
```rust
133133
impl Solution {
134134
pub fn reverse_prefix(word: String, ch: char) -> String {
135135
match word.find(ch) {

‎solution/2000-2099/2000.Reverse Prefix of Word/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<pre>
1919
<strong>Input:</strong> word = &quot;<u>abcd</u>efd&quot;, ch = &quot;d&quot;
2020
<strong>Output:</strong> &quot;<u>dcba</u>efd&quot;
21-
<strong>Explanation:</strong>&nbsp;The first occurrence of &quot;d&quot; is at index 3.
21+
<strong>Explanation:</strong>&nbsp;The first occurrence of &quot;d&quot; is at index 3.
2222
Reverse the part of word from 0 to 3 (inclusive), the resulting string is &quot;dcbaefd&quot;.
2323
</pre>
2424

@@ -122,7 +122,7 @@ function reversePrefix(word: string, ch: string): string {
122122

123123
### **Rust**
124124

125-
```rs
125+
```rust
126126
impl Solution {
127127
pub fn reverse_prefix(word: String, ch: char) -> String {
128128
match word.find(ch) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /