Skip to content

Commit 636bed6

Browse files
committed
feat: add solutions to lcof problem: No.68 - |
1 parent cc34eee commit 636bed6

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,120 @@ var lowestCommonAncestor = function (root, p, q) {
153153
};
154154
```
155155

156+
### **TypeScript**
157+
158+
递归:
159+
160+
```ts
161+
/**
162+
* Definition for a binary tree node.
163+
* class TreeNode {
164+
* val: number
165+
* left: TreeNode | null
166+
* right: TreeNode | null
167+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
168+
* this.val = (val===undefined ? 0 : val)
169+
* this.left = (left===undefined ? null : left)
170+
* this.right = (right===undefined ? null : right)
171+
* }
172+
* }
173+
*/
174+
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
175+
if (root == null) {
176+
return root
177+
}
178+
if (root.val > p.val && root.val > q.val) {
179+
return lowestCommonAncestor(root.left, p, q)
180+
}
181+
if (root.val < p.val && root.val < q.val) {
182+
return lowestCommonAncestor(root.right, p, q)
183+
}
184+
return root
185+
};
186+
```
187+
188+
循环:
189+
190+
```ts
191+
/**
192+
* Definition for a binary tree node.
193+
* class TreeNode {
194+
* val: number
195+
* left: TreeNode | null
196+
* right: TreeNode | null
197+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
198+
* this.val = (val===undefined ? 0 : val)
199+
* this.left = (left===undefined ? null : left)
200+
* this.right = (right===undefined ? null : right)
201+
* }
202+
* }
203+
*/
204+
function lowestCommonAncestor(
205+
root: TreeNode | null,
206+
p: TreeNode | null,
207+
q: TreeNode | null,
208+
): TreeNode | null {
209+
if (root == null) {
210+
return root;
211+
}
212+
while (true) {
213+
if (root.val > p.val && root.val > q.val) {
214+
root = root.left;
215+
} else if (root.val < p.val && root.val < q.val) {
216+
root = root.right;
217+
} else {
218+
return root;
219+
}
220+
}
221+
}
222+
```
223+
224+
### **Rust**
225+
226+
```rust
227+
// Definition for a binary tree node.
228+
// #[derive(Debug, PartialEq, Eq)]
229+
// pub struct TreeNode {
230+
// pub val: i32,
231+
// pub left: Option<Rc<RefCell<TreeNode>>>,
232+
// pub right: Option<Rc<RefCell<TreeNode>>>,
233+
// }
234+
//
235+
// impl TreeNode {
236+
// #[inline]
237+
// pub fn new(val: i32) -> Self {
238+
// TreeNode {
239+
// val,
240+
// left: None,
241+
// right: None
242+
// }
243+
// }
244+
// }
245+
use std::rc::Rc;
246+
use std::cell::RefCell;
247+
use std::cmp::Ordering;
248+
impl Solution {
249+
pub fn lowest_common_ancestor(
250+
mut root: Option<Rc<RefCell<TreeNode>>>,
251+
p: Option<Rc<RefCell<TreeNode>>>,
252+
q: Option<Rc<RefCell<TreeNode>>>,
253+
) -> Option<Rc<RefCell<TreeNode>>> {
254+
let p = p.unwrap().borrow().val;
255+
let q = q.unwrap().borrow().val;
256+
loop {
257+
let mut cur = root.as_ref().unwrap().borrow().val;
258+
match (cur.cmp(&p), cur.cmp(&q)) {
259+
(Ordering::Less, Ordering::Less) => root = root.unwrap().borrow().right.clone(),
260+
(Ordering::Greater, Ordering::Greater) => {
261+
root = root.unwrap().borrow().left.clone()
262+
}
263+
(_, _) => break root,
264+
}
265+
}
266+
}
267+
}
268+
```
269+
156270
### **...**
157271

158272
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use std::cmp::Ordering;
22+
impl Solution {
23+
pub fn lowest_common_ancestor(
24+
mut root: Option<Rc<RefCell<TreeNode>>>,
25+
p: Option<Rc<RefCell<TreeNode>>>,
26+
q: Option<Rc<RefCell<TreeNode>>>,
27+
) -> Option<Rc<RefCell<TreeNode>>> {
28+
let p = p.unwrap().borrow().val;
29+
let q = q.unwrap().borrow().val;
30+
loop {
31+
let mut cur = root.as_ref().unwrap().borrow().val;
32+
match (cur.cmp(&p), cur.cmp(&q)) {
33+
(Ordering::Less, Ordering::Less) => root = root.unwrap().borrow().right.clone(),
34+
(Ordering::Greater, Ordering::Greater) => {
35+
root = root.unwrap().borrow().left.clone()
36+
}
37+
(_, _) => break root,
38+
}
39+
}
40+
}
41+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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) {
20+
return root;
21+
}
22+
while (true) {
23+
if (root.val > p.val && root.val > q.val) {
24+
root = root.left;
25+
} else if (root.val < p.val && root.val < q.val) {
26+
root = root.right;
27+
} else {
28+
return root;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)