Skip to content

Commit 4b3bc8a

Browse files
committed
feat: add solutions to lc problems: No.0236,0297
- No.0236.Lowest Common Ancestor of a Binary Tree - No.0297.Serialize and Deserialize Binary Tree
1 parent 58c37f6 commit 4b3bc8a

File tree

8 files changed

+725
-0
lines changed

8 files changed

+725
-0
lines changed

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,99 @@ var lowestCommonAncestor = function (root, p, q) {
143143
};
144144
```
145145

146+
### **TypeScript**
147+
148+
```ts
149+
/**
150+
* Definition for a binary tree node.
151+
* class TreeNode {
152+
* val: number
153+
* left: TreeNode | null
154+
* right: TreeNode | null
155+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
156+
* this.val = (val===undefined ? 0 : val)
157+
* this.left = (left===undefined ? null : left)
158+
* this.right = (right===undefined ? null : right)
159+
* }
160+
* }
161+
*/
162+
163+
function lowestCommonAncestor(
164+
root: TreeNode | null,
165+
p: TreeNode | null,
166+
q: TreeNode | null,
167+
): TreeNode | null {
168+
const find = (root: TreeNode | null) => {
169+
if (root == null || root == p || root == q) {
170+
return root;
171+
}
172+
const left = find(root.left);
173+
const right = find(root.right);
174+
if (left != null && right != null) {
175+
return root;
176+
}
177+
if (left != null) {
178+
return left;
179+
}
180+
return right;
181+
};
182+
return find(root);
183+
}
184+
```
185+
186+
### **Rust**
187+
188+
```rust
189+
// Definition for a binary tree node.
190+
// #[derive(Debug, PartialEq, Eq)]
191+
// pub struct TreeNode {
192+
// pub val: i32,
193+
// pub left: Option<Rc<RefCell<TreeNode>>>,
194+
// pub right: Option<Rc<RefCell<TreeNode>>>,
195+
// }
196+
//
197+
// impl TreeNode {
198+
// #[inline]
199+
// pub fn new(val: i32) -> Self {
200+
// TreeNode {
201+
// val,
202+
// left: None,
203+
// right: None
204+
// }
205+
// }
206+
// }
207+
use std::rc::Rc;
208+
use std::cell::RefCell;
209+
impl Solution {
210+
fn find(
211+
root: &Option<Rc<RefCell<TreeNode>>>,
212+
p: &Option<Rc<RefCell<TreeNode>>>,
213+
q: &Option<Rc<RefCell<TreeNode>>>,
214+
) -> Option<Rc<RefCell<TreeNode>>> {
215+
if root.is_none() || root == p || root == q {
216+
return root.clone();
217+
}
218+
let node = root.as_ref().unwrap().borrow();
219+
let left = Self::find(&node.left, p, q);
220+
let right = Self::find(&node.right, p, q);
221+
match (left.is_some(), right.is_some()) {
222+
(true, false) => left,
223+
(false, true) => right,
224+
(false, false) => None,
225+
(true, true) => root.clone(),
226+
}
227+
}
228+
229+
pub fn lowest_common_ancestor(
230+
root: Option<Rc<RefCell<TreeNode>>>,
231+
p: Option<Rc<RefCell<TreeNode>>>,
232+
q: Option<Rc<RefCell<TreeNode>>>,
233+
) -> Option<Rc<RefCell<TreeNode>>> {
234+
Self::find(&root, &p, &q)
235+
}
236+
}
237+
```
238+
146239
### **...**
147240

148241
```

solution/0200-0299/0236.Lowest Common Ancestor of a Binary Tree/README_EN.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,99 @@ var lowestCommonAncestor = function (root, p, q) {
120120
};
121121
```
122122

123+
### **TypeScript**
124+
125+
```ts
126+
/**
127+
* Definition for a binary tree node.
128+
* class TreeNode {
129+
* val: number
130+
* left: TreeNode | null
131+
* right: TreeNode | null
132+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
133+
* this.val = (val===undefined ? 0 : val)
134+
* this.left = (left===undefined ? null : left)
135+
* this.right = (right===undefined ? null : right)
136+
* }
137+
* }
138+
*/
139+
140+
function lowestCommonAncestor(
141+
root: TreeNode | null,
142+
p: TreeNode | null,
143+
q: TreeNode | null,
144+
): TreeNode | null {
145+
const find = (root: TreeNode | null) => {
146+
if (root == null || root == p || root == q) {
147+
return root;
148+
}
149+
const left = find(root.left);
150+
const right = find(root.right);
151+
if (left != null && right != null) {
152+
return root;
153+
}
154+
if (left != null) {
155+
return left;
156+
}
157+
return right;
158+
};
159+
return find(root);
160+
}
161+
```
162+
163+
### **Rust**
164+
165+
```rust
166+
// Definition for a binary tree node.
167+
// #[derive(Debug, PartialEq, Eq)]
168+
// pub struct TreeNode {
169+
// pub val: i32,
170+
// pub left: Option<Rc<RefCell<TreeNode>>>,
171+
// pub right: Option<Rc<RefCell<TreeNode>>>,
172+
// }
173+
//
174+
// impl TreeNode {
175+
// #[inline]
176+
// pub fn new(val: i32) -> Self {
177+
// TreeNode {
178+
// val,
179+
// left: None,
180+
// right: None
181+
// }
182+
// }
183+
// }
184+
use std::rc::Rc;
185+
use std::cell::RefCell;
186+
impl Solution {
187+
fn find(
188+
root: &Option<Rc<RefCell<TreeNode>>>,
189+
p: &Option<Rc<RefCell<TreeNode>>>,
190+
q: &Option<Rc<RefCell<TreeNode>>>,
191+
) -> Option<Rc<RefCell<TreeNode>>> {
192+
if root.is_none() || root == p || root == q {
193+
return root.clone();
194+
}
195+
let node = root.as_ref().unwrap().borrow();
196+
let left = Self::find(&node.left, p, q);
197+
let right = Self::find(&node.right, p, q);
198+
match (left.is_some(), right.is_some()) {
199+
(true, false) => left,
200+
(false, true) => right,
201+
(false, false) => None,
202+
(true, true) => root.clone(),
203+
}
204+
}
205+
206+
pub fn lowest_common_ancestor(
207+
root: Option<Rc<RefCell<TreeNode>>>,
208+
p: Option<Rc<RefCell<TreeNode>>>,
209+
q: Option<Rc<RefCell<TreeNode>>>,
210+
) -> Option<Rc<RefCell<TreeNode>>> {
211+
Self::find(&root, &p, &q)
212+
}
213+
}
214+
```
215+
123216
### **...**
124217

125218
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
fn find(
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.clone();
29+
}
30+
let node = root.as_ref().unwrap().borrow();
31+
let left = Self::find(&node.left, p, q);
32+
let right = Self::find(&node.right, p, q);
33+
match (left.is_some(), right.is_some()) {
34+
(true, false) => left,
35+
(false, true) => right,
36+
(false, false) => None,
37+
(true, true) => root.clone(),
38+
}
39+
}
40+
41+
pub fn lowest_common_ancestor(
42+
root: Option<Rc<RefCell<TreeNode>>>,
43+
p: Option<Rc<RefCell<TreeNode>>>,
44+
q: Option<Rc<RefCell<TreeNode>>>,
45+
) -> Option<Rc<RefCell<TreeNode>>> {
46+
Self::find(&root, &p, &q)
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
15+
function lowestCommonAncestor(
16+
root: TreeNode | null,
17+
p: TreeNode | null,
18+
q: TreeNode | null,
19+
): TreeNode | null {
20+
const find = (root: TreeNode | null) => {
21+
if (root == null || root == p || root == q) {
22+
return root;
23+
}
24+
const left = find(root.left);
25+
const right = find(root.right);
26+
if (left != null && right != null) {
27+
return root;
28+
}
29+
if (left != null) {
30+
return left;
31+
}
32+
return right;
33+
};
34+
return find(root);
35+
}

0 commit comments

Comments
 (0)