Skip to content

Commit 9b8b726

Browse files
committed
feat: add solutions to lcof problem: No.68 - ||
面试题68 - II. 二叉树的最近公共祖先
1 parent e826df0 commit 9b8b726

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

lcof/面试题68 - II. 二叉树的最近公共祖先/README.md

+91
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,97 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
192192
}
193193
```
194194

195+
### **TypeScript**
196+
197+
```ts
198+
/**
199+
* Definition for a binary tree node.
200+
* class TreeNode {
201+
* val: number
202+
* left: TreeNode | null
203+
* right: TreeNode | null
204+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
205+
* this.val = (val===undefined ? 0 : val)
206+
* this.left = (left===undefined ? null : left)
207+
* this.right = (right===undefined ? null : right)
208+
* }
209+
* }
210+
*/
211+
function lowestCommonAncestor(
212+
root: TreeNode | null,
213+
p: TreeNode | null,
214+
q: TreeNode | null,
215+
): TreeNode | null {
216+
if (root == null || root === p || root === q) {
217+
return root;
218+
}
219+
const left = lowestCommonAncestor(root.left, p, q);
220+
const right = lowestCommonAncestor(root.right, p, q);
221+
if (left == null && right == null) {
222+
return null;
223+
}
224+
if (left == null) {
225+
return right;
226+
}
227+
if (right == null) {
228+
return left;
229+
}
230+
return root;
231+
}
232+
```
233+
234+
### **Rust**
235+
236+
```rust
237+
// Definition for a binary tree node.
238+
// #[derive(Debug, PartialEq, Eq)]
239+
// pub struct TreeNode {
240+
// pub val: i32,
241+
// pub left: Option<Rc<RefCell<TreeNode>>>,
242+
// pub right: Option<Rc<RefCell<TreeNode>>>,
243+
// }
244+
//
245+
// impl TreeNode {
246+
// #[inline]
247+
// pub fn new(val: i32) -> Self {
248+
// TreeNode {
249+
// val,
250+
// left: None,
251+
// right: None
252+
// }
253+
// }
254+
// }
255+
use std::rc::Rc;
256+
use std::cell::RefCell;
257+
impl Solution {
258+
pub fn lowest_common_ancestor(
259+
root: Option<Rc<RefCell<TreeNode>>>,
260+
p: Option<Rc<RefCell<TreeNode>>>,
261+
q: Option<Rc<RefCell<TreeNode>>>,
262+
) -> Option<Rc<RefCell<TreeNode>>> {
263+
if root.is_none() || root == p || root == q {
264+
return root;
265+
}
266+
let left = Self::lowest_common_ancestor(
267+
root.as_ref().unwrap().borrow_mut().left.take(),
268+
p.clone(),
269+
q.clone(),
270+
);
271+
let right = Self::lowest_common_ancestor(
272+
root.as_ref().unwrap().borrow_mut().right.take(),
273+
p.clone(),
274+
q.clone(),
275+
);
276+
match (left.is_none(), right.is_none()) {
277+
(true, false) => right,
278+
(false, true) => left,
279+
(false, false) => root,
280+
(true, true) => None,
281+
}
282+
}
283+
}
284+
```
285+
195286
### **...**
196287

197288
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
pub fn lowest_common_ancestor(
23+
root: Option<Rc<RefCell<TreeNode>>>,
24+
p: Option<Rc<RefCell<TreeNode>>>,
25+
q: Option<Rc<RefCell<TreeNode>>>,
26+
) -> Option<Rc<RefCell<TreeNode>>> {
27+
if root.is_none() || root == p || root == q {
28+
return root;
29+
}
30+
let left = Self::lowest_common_ancestor(
31+
root.as_ref().unwrap().borrow_mut().left.take(),
32+
p.clone(),
33+
q.clone(),
34+
);
35+
let right = Self::lowest_common_ancestor(
36+
root.as_ref().unwrap().borrow_mut().right.take(),
37+
p.clone(),
38+
q.clone(),
39+
);
40+
match (left.is_none(), right.is_none()) {
41+
(true, false) => right,
42+
(false, true) => left,
43+
(false, false) => root,
44+
(true, true) => None,
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
function lowestCommonAncestor(
15+
root: TreeNode | null,
16+
p: TreeNode | null,
17+
q: TreeNode | null,
18+
): TreeNode | null {
19+
if (root == null || root === p || root === q) {
20+
return root;
21+
}
22+
const left = lowestCommonAncestor(root.left, p, q);
23+
const right = lowestCommonAncestor(root.right, p, q);
24+
if (left == null && right == null) {
25+
return null;
26+
}
27+
if (left == null) {
28+
return right;
29+
}
30+
if (right == null) {
31+
return left;
32+
}
33+
return root;
34+
}

0 commit comments

Comments
 (0)