Skip to content

Commit 57ec4f2

Browse files
committed
feat: update solutions to lcof problems
1 parent e53192c commit 57ec4f2

File tree

9 files changed

+116
-154
lines changed

9 files changed

+116
-154
lines changed

lcof/面试题26. 树的子结构/README.md

+19-23
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,17 @@ function isSubStructure(A: TreeNode | null, B: TreeNode | null): boolean {
199199
if (A == null || B == null) {
200200
return false;
201201
}
202-
if (A.val == B.val && exam(A.left, B.left) && exam(A.right, B.right)) {
203-
return true;
204-
}
205-
return isSubStructure(A.left, B) || isSubStructure(A.right, B);
202+
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
206203
}
207204

208-
function exam(A: TreeNode | null, B: TreeNode | null) {
205+
function dfs(A: TreeNode | null, B: TreeNode | null) {
209206
if (B == null) {
210207
return true;
211208
}
212209
if (A == null) {
213210
return false;
214211
}
215-
return A.val === B.val && exam(A.left, B.left) && exam(A.right, B.right);
212+
return A.val === B.val && dfs(A.left, B.left) && dfs(A.right, B.right);
216213
}
217214
```
218215

@@ -239,38 +236,37 @@ function exam(A: TreeNode | null, B: TreeNode | null) {
239236
// }
240237
use std::rc::Rc;
241238
use std::cell::RefCell;
242-
243239
impl Solution {
244240
pub fn is_sub_structure(
245241
a: Option<Rc<RefCell<TreeNode>>>,
246242
b: Option<Rc<RefCell<TreeNode>>>,
243+
) -> bool {
244+
Self::is_sub_structure_help(&a, &b)
245+
}
246+
247+
fn is_sub_structure_help(
248+
a: &Option<Rc<RefCell<TreeNode>>>,
249+
b: &Option<Rc<RefCell<TreeNode>>>,
247250
) -> bool {
248251
if a.is_none() || b.is_none() {
249252
return false;
250253
}
251-
let a_ref = a.as_ref().unwrap().borrow();
252-
let b_ref = b.as_ref().unwrap().borrow();
253254

254-
if a_ref.val == b_ref.val
255-
&& Solution::exam(&a_ref.left, &b_ref.left)
256-
&& Solution::exam(&a_ref.right, &b_ref.right)
257-
{
258-
return true;
259-
}
260-
Solution::is_sub_structure(a_ref.left.clone(), b.clone())
261-
|| Solution::is_sub_structure(a_ref.right.clone(), b.clone())
255+
Self::dfs(a, b)
256+
|| Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().left, b)
257+
|| Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().right, b)
262258
}
263259

264-
fn exam(a: &Option<Rc<RefCell<TreeNode>>>, b: &Option<Rc<RefCell<TreeNode>>>) -> bool {
265-
if a.is_none() && b.is_some() {
266-
return false;
267-
}
268-
if b.is_none() || a.is_none() && b.is_none() {
260+
fn dfs(a: &Option<Rc<RefCell<TreeNode>>>, b: &Option<Rc<RefCell<TreeNode>>>) -> bool {
261+
if b.is_none() {
269262
return true;
270263
}
264+
if a.is_none() {
265+
return false;
266+
}
271267
let a = a.as_ref().unwrap().borrow();
272268
let b = b.as_ref().unwrap().borrow();
273-
a.val == b.val && Solution::exam(&a.left, &b.left) && Solution::exam(&a.right, &b.right)
269+
a.val == b.val && Self::dfs(&a.left, &b.left) && Self::dfs(&a.right, &b.right)
274270
}
275271
}
276272
```

lcof/面试题26. 树的子结构/Solution.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,36 @@
1818
// }
1919
use std::rc::Rc;
2020
use std::cell::RefCell;
21-
2221
impl Solution {
2322
pub fn is_sub_structure(
2423
a: Option<Rc<RefCell<TreeNode>>>,
2524
b: Option<Rc<RefCell<TreeNode>>>,
25+
) -> bool {
26+
Self::is_sub_structure_help(&a, &b)
27+
}
28+
29+
fn is_sub_structure_help(
30+
a: &Option<Rc<RefCell<TreeNode>>>,
31+
b: &Option<Rc<RefCell<TreeNode>>>,
2632
) -> bool {
2733
if a.is_none() || b.is_none() {
2834
return false;
2935
}
30-
let a_ref = a.as_ref().unwrap().borrow();
31-
let b_ref = b.as_ref().unwrap().borrow();
3236

33-
if a_ref.val == b_ref.val
34-
&& Solution::exam(&a_ref.left, &b_ref.left)
35-
&& Solution::exam(&a_ref.right, &b_ref.right)
36-
{
37-
return true;
38-
}
39-
Solution::is_sub_structure(a_ref.left.clone(), b.clone())
40-
|| Solution::is_sub_structure(a_ref.right.clone(), b.clone())
37+
Self::dfs(a, b)
38+
|| Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().left, b)
39+
|| Self::is_sub_structure_help(&a.as_ref().unwrap().borrow().right, b)
4140
}
4241

43-
fn exam(a: &Option<Rc<RefCell<TreeNode>>>, b: &Option<Rc<RefCell<TreeNode>>>) -> bool {
44-
if a.is_none() && b.is_some() {
45-
return false;
46-
}
47-
if b.is_none() || a.is_none() && b.is_none() {
42+
fn dfs(a: &Option<Rc<RefCell<TreeNode>>>, b: &Option<Rc<RefCell<TreeNode>>>) -> bool {
43+
if b.is_none() {
4844
return true;
4945
}
46+
if a.is_none() {
47+
return false;
48+
}
5049
let a = a.as_ref().unwrap().borrow();
5150
let b = b.as_ref().unwrap().borrow();
52-
a.val == b.val && Solution::exam(&a.left, &b.left) && Solution::exam(&a.right, &b.right)
51+
a.val == b.val && Self::dfs(&a.left, &b.left) && Self::dfs(&a.right, &b.right)
5352
}
5453
}

lcof/面试题26. 树的子结构/Solution.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@ function isSubStructure(A: TreeNode | null, B: TreeNode | null): boolean {
1616
if (A == null || B == null) {
1717
return false;
1818
}
19-
if (A.val == B.val && exam(A.left, B.left) && exam(A.right, B.right)) {
20-
return true;
21-
}
22-
return isSubStructure(A.left, B) || isSubStructure(A.right, B);
19+
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
2320
}
2421

25-
function exam(A: TreeNode | null, B: TreeNode | null) {
22+
function dfs(A: TreeNode | null, B: TreeNode | null) {
2623
if (B == null) {
2724
return true;
2825
}
2926
if (A == null) {
3027
return false;
3128
}
32-
return A.val === B.val && exam(A.left, B.left) && exam(A.right, B.right);
29+
return A.val === B.val && dfs(A.left, B.left) && dfs(A.right, B.right);
3330
}

lcof/面试题27. 二叉树的镜像/README.md

+12-13
Original file line numberDiff line numberDiff line change
@@ -211,23 +211,22 @@ function mirrorTree(root: TreeNode | null): TreeNode | null {
211211
// }
212212
// }
213213
// }
214-
use std::mem;
215214
use std::rc::Rc;
216215
use std::cell::RefCell;
217-
218216
impl Solution {
219-
pub fn mirror_tree(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
220-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) {
221-
if let Some(node) = root {
222-
let mut node = node.borrow_mut();
223-
let lt = mem::replace(&mut node.left, None);
224-
let rt = mem::replace(&mut node.right, lt);
225-
mem::replace(&mut node.left, rt);
226-
dfs(&node.left);
227-
dfs(&node.right);
228-
}
217+
fn dfs(root: &mut Option<Rc<RefCell<TreeNode>>>) {
218+
if let Some(node) = root {
219+
let mut node = node.borrow_mut();
220+
let temp = node.left.take();
221+
node.left = node.right.take();
222+
node.right = temp;
223+
Self::dfs(&mut node.left);
224+
Self::dfs(&mut node.right);
229225
}
230-
dfs(&root);
226+
}
227+
228+
pub fn mirror_tree(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
229+
Self::dfs(&mut root);
231230
root
232231
}
233232
}

lcof/面试题27. 二叉树的镜像/Solution.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,22 @@
1616
// }
1717
// }
1818
// }
19-
use std::mem;
2019
use std::rc::Rc;
2120
use std::cell::RefCell;
22-
2321
impl Solution {
24-
pub fn mirror_tree(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
25-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) {
26-
if let Some(node) = root {
27-
let mut node = node.borrow_mut();
28-
let lt = mem::replace(&mut node.left, None);
29-
let rt = mem::replace(&mut node.right, lt);
30-
mem::replace(&mut node.left, rt);
31-
dfs(&node.left);
32-
dfs(&node.right);
33-
}
22+
fn dfs(root: &mut Option<Rc<RefCell<TreeNode>>>) {
23+
if let Some(node) = root {
24+
let mut node = node.borrow_mut();
25+
let temp = node.left.take();
26+
node.left = node.right.take();
27+
node.right = temp;
28+
Self::dfs(&mut node.left);
29+
Self::dfs(&mut node.right);
3430
}
35-
dfs(&root);
31+
}
32+
33+
pub fn mirror_tree(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
34+
Self::dfs(&mut root);
3635
root
3736
}
3837
}

lcof/面试题28. 对称的二叉树/README.md

+27-42
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,17 @@ func isSymme(left *TreeNode, right *TreeNode) bool {
160160
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
161161
* };
162162
*/
163-
164163
class Solution {
165164
public:
166-
bool isSymmetric(TreeNode* a, TreeNode* b) {
167-
// 均为空,则直接返回true。有且仅有一个不为空,则返回false
168-
if (a == nullptr && b == nullptr) {
165+
bool isSymmetric(TreeNode* left, TreeNode* right) {
166+
// 均为空,则直接返回 true。有且仅有一个不为空,则返回 false
167+
if (left == nullptr && right == nullptr) {
169168
return true;
170-
} else if (a == nullptr && b != nullptr) {
171-
return false;
172-
} else if (a != nullptr && b == nullptr) {
169+
}
170+
if (left == nullptr || right == nullptr || left->val != right->val) {
173171
return false;
174172
}
175-
176-
// 判定值是否相等,和下面的节点是否对称
177-
return (a->val == b->val) && isSymmetric(a->left, b->right) && isSymmetric(a->right, b->left);
173+
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
178174
}
179175

180176
bool isSymmetric(TreeNode* root) {
@@ -185,7 +181,6 @@ public:
185181
return isSymmetric(root->left, root->right);
186182
}
187183
};
188-
189184
```
190185

191186
### **TypeScript**
@@ -209,19 +204,14 @@ function isSymmetric(root: TreeNode | null): boolean {
209204
if (root == null) {
210205
return true;
211206
}
212-
const dfs = (l: TreeNode | null, r: TreeNode | null) => {
213-
if (l == null && r == null) {
207+
const dfs = (left: TreeNode | null, right: TreeNode | null) => {
208+
if (left == null && right == null) {
214209
return true;
215210
}
216-
if (l == null || r == null) {
211+
if (left == null || right == null || left.val != right.val) {
217212
return false;
218213
}
219-
return (
220-
l.val == r.val &&
221-
dfs(l.left, r.right) &&
222-
dfs(l.right, r.left) &&
223-
true
224-
);
214+
return dfs(left.left, right.right) && dfs(left.right, right.left);
225215
};
226216
return dfs(root.left, root.right);
227217
}
@@ -248,34 +238,29 @@ function isSymmetric(root: TreeNode | null): boolean {
248238
// }
249239
// }
250240
// }
251-
use std::cell::RefCell;
252241
use std::rc::Rc;
242+
use std::cell::RefCell;
253243
impl Solution {
244+
fn dfs(left: &Option<Rc<RefCell<TreeNode>>>, right: &Option<Rc<RefCell<TreeNode>>>) -> bool {
245+
if left.is_none() && right.is_none() {
246+
return true;
247+
}
248+
if left.is_none() || right.is_none() {
249+
return false;
250+
}
251+
let l = left.as_ref().unwrap().borrow();
252+
let r = right.as_ref().unwrap().borrow();
253+
l.val == r.val && Self::dfs(&l.left, &r.right) && Self::dfs(&l.right, &r.left)
254+
}
255+
254256
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
255-
match root {
256-
None => true,
257-
Some(root) => {
258-
fn dfs(
259-
l: &Option<Rc<RefCell<TreeNode>>>,
260-
r: &Option<Rc<RefCell<TreeNode>>>,
261-
) -> bool {
262-
if l.is_none() && r.is_none() {
263-
return true;
264-
}
265-
if l.is_none() || r.is_none() {
266-
return false;
267-
}
268-
let l = l.as_ref().unwrap().borrow();
269-
let r = r.as_ref().unwrap().borrow();
270-
l.val == r.val && dfs(&l.left, &r.right) && dfs(&l.right, &r.left) && true
271-
}
272-
let node = root.borrow();
273-
dfs(&node.left, &node.right)
274-
}
257+
if root.is_none() {
258+
return true;
275259
}
260+
let node = root.as_ref().unwrap().borrow();
261+
Self::dfs(&node.left, &node.right)
276262
}
277263
}
278-
279264
```
280265

281266
### **...**

lcof/面试题28. 对称的二叉树/Solution.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,17 @@
77
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
88
* };
99
*/
10-
1110
class Solution {
1211
public:
13-
bool isSymmetric(TreeNode* a, TreeNode* b) {
14-
// 均为空,则直接返回true。有且仅有一个不为空,则返回false
15-
if (a == nullptr && b == nullptr) {
12+
bool isSymmetric(TreeNode* left, TreeNode* right) {
13+
// 均为空,则直接返回 true。有且仅有一个不为空,则返回 false
14+
if (left == nullptr && right == nullptr) {
1615
return true;
17-
} else if (a == nullptr && b != nullptr) {
18-
return false;
19-
} else if (a != nullptr && b == nullptr) {
16+
}
17+
if (left == nullptr || right == nullptr || left->val != right->val) {
2018
return false;
2119
}
22-
23-
// 判定值是否相等,和下面的节点是否对称
24-
return (a->val == b->val) && isSymmetric(a->left, b->right) && isSymmetric(a->right, b->left);
20+
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
2521
}
2622

2723
bool isSymmetric(TreeNode* root) {

0 commit comments

Comments
 (0)