Skip to content

Commit bd392b4

Browse files
committed
feat: add solutions to lc problems: No.0109, 0976, 1779
- No.0109.Convert Sorted List to Binary Search Tree - No.0976.Largest Perimeter Triangle - No.1779.Find Nearest Point That Has the Same X or Y Coordinate
1 parent c45bac4 commit bd392b4

File tree

11 files changed

+575
-0
lines changed

11 files changed

+575
-0
lines changed

solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,167 @@ var sortedListToBST = function (head) {
219219
};
220220
```
221221

222+
### **TypeScript**
223+
224+
```ts
225+
/**
226+
* Definition for singly-linked list.
227+
* class ListNode {
228+
* val: number
229+
* next: ListNode | null
230+
* constructor(val?: number, next?: ListNode | null) {
231+
* this.val = (val===undefined ? 0 : val)
232+
* this.next = (next===undefined ? null : next)
233+
* }
234+
* }
235+
*/
236+
237+
/**
238+
* Definition for a binary tree node.
239+
* class TreeNode {
240+
* val: number
241+
* left: TreeNode | null
242+
* right: TreeNode | null
243+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
244+
* this.val = (val===undefined ? 0 : val)
245+
* this.left = (left===undefined ? null : left)
246+
* this.right = (right===undefined ? null : right)
247+
* }
248+
* }
249+
*/
250+
251+
const find = (start: ListNode | null, end: ListNode | null) => {
252+
let fast = start;
253+
let slow = start;
254+
while (fast !== end && fast.next !== end) {
255+
fast = fast.next.next;
256+
slow = slow.next;
257+
}
258+
return slow;
259+
};
260+
261+
const build = (start: ListNode | null, end: ListNode | null) => {
262+
if (start == end) {
263+
return null;
264+
}
265+
const node = find(start, end);
266+
return new TreeNode(node.val, build(start, node), build(node.next, end));
267+
};
268+
269+
function sortedListToBST(head: ListNode | null): TreeNode | null {
270+
return build(head, null);
271+
}
272+
```
273+
274+
### **Rust**
275+
276+
```rust
277+
// Definition for singly-linked list.
278+
// #[derive(PartialEq, Eq, Clone, Debug)]
279+
// pub struct ListNode {
280+
// pub val: i32,
281+
// pub next: Option<Box<ListNode>>
282+
// }
283+
//
284+
// impl ListNode {
285+
// #[inline]
286+
// fn new(val: i32) -> Self {
287+
// ListNode {
288+
// next: None,
289+
// val
290+
// }
291+
// }
292+
// }
293+
// Definition for a binary tree node.
294+
// #[derive(Debug, PartialEq, Eq)]
295+
// pub struct TreeNode {
296+
// pub val: i32,
297+
// pub left: Option<Rc<RefCell<TreeNode>>>,
298+
// pub right: Option<Rc<RefCell<TreeNode>>>,
299+
// }
300+
//
301+
// impl TreeNode {
302+
// #[inline]
303+
// pub fn new(val: i32) -> Self {
304+
// TreeNode {
305+
// val,
306+
// left: None,
307+
// right: None
308+
// }
309+
// }
310+
// }
311+
use std::rc::Rc;
312+
use std::cell::RefCell;
313+
impl Solution {
314+
fn build(vals: &Vec<i32>, start: usize, end: usize) -> Option<Rc<RefCell<TreeNode>>> {
315+
if (start == end) {
316+
return None;
317+
}
318+
let mid = (start + end) >> 1;
319+
Some(Rc::new(RefCell::new(TreeNode {
320+
val: vals[mid],
321+
left: Self::build(vals, start, mid),
322+
right: Self::build(vals, mid + 1, end),
323+
})))
324+
}
325+
326+
pub fn sorted_list_to_bst(head: Option<Box<ListNode>>) -> Option<Rc<RefCell<TreeNode>>> {
327+
let mut vals = Vec::new();
328+
let mut cur = &head;
329+
while let Some(node) = cur {
330+
vals.push(node.val);
331+
cur = &node.next;
332+
}
333+
Self::build(&vals, 0, vals.len())
334+
}
335+
}
336+
```
337+
338+
### **C**
339+
340+
```c
341+
/**
342+
* Definition for singly-linked list.
343+
* struct ListNode {
344+
* int val;
345+
* struct ListNode *next;
346+
* };
347+
*/
348+
/**
349+
* Definition for a binary tree node.
350+
* struct TreeNode {
351+
* int val;
352+
* struct TreeNode *left;
353+
* struct TreeNode *right;
354+
* };
355+
*/
356+
struct ListNode *find(struct ListNode *start, struct ListNode *end) {
357+
struct ListNode *fast = start;
358+
struct ListNode *slow = start;
359+
while (fast != end && fast->next != end) {
360+
fast = fast->next->next;
361+
slow = slow->next;
362+
}
363+
return slow;
364+
}
365+
366+
struct TreeNode *bulid(struct ListNode *start, struct ListNode *end) {
367+
if (start == end) {
368+
return NULL;
369+
}
370+
struct ListNode *node = find(start, end);
371+
struct TreeNode *ans = malloc(sizeof(struct TreeNode));
372+
ans->val = node->val;
373+
ans->left = bulid(start, node);
374+
ans->right = bulid(node->next, end);
375+
return ans;
376+
}
377+
378+
struct TreeNode *sortedListToBST(struct ListNode *head) {
379+
return bulid(head, NULL);
380+
}
381+
```
382+
222383
### **...**
223384
224385
```

solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,167 @@ var sortedListToBST = function (head) {
205205
};
206206
```
207207

208+
### **TypeScript**
209+
210+
```ts
211+
/**
212+
* Definition for singly-linked list.
213+
* class ListNode {
214+
* val: number
215+
* next: ListNode | null
216+
* constructor(val?: number, next?: ListNode | null) {
217+
* this.val = (val===undefined ? 0 : val)
218+
* this.next = (next===undefined ? null : next)
219+
* }
220+
* }
221+
*/
222+
223+
/**
224+
* Definition for a binary tree node.
225+
* class TreeNode {
226+
* val: number
227+
* left: TreeNode | null
228+
* right: TreeNode | null
229+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
230+
* this.val = (val===undefined ? 0 : val)
231+
* this.left = (left===undefined ? null : left)
232+
* this.right = (right===undefined ? null : right)
233+
* }
234+
* }
235+
*/
236+
237+
const find = (start: ListNode | null, end: ListNode | null) => {
238+
let fast = start;
239+
let slow = start;
240+
while (fast !== end && fast.next !== end) {
241+
fast = fast.next.next;
242+
slow = slow.next;
243+
}
244+
return slow;
245+
};
246+
247+
const build = (start: ListNode | null, end: ListNode | null) => {
248+
if (start == end) {
249+
return null;
250+
}
251+
const node = find(start, end);
252+
return new TreeNode(node.val, build(start, node), build(node.next, end));
253+
};
254+
255+
function sortedListToBST(head: ListNode | null): TreeNode | null {
256+
return build(head, null);
257+
}
258+
```
259+
260+
### **Rust**
261+
262+
```rust
263+
// Definition for singly-linked list.
264+
// #[derive(PartialEq, Eq, Clone, Debug)]
265+
// pub struct ListNode {
266+
// pub val: i32,
267+
// pub next: Option<Box<ListNode>>
268+
// }
269+
//
270+
// impl ListNode {
271+
// #[inline]
272+
// fn new(val: i32) -> Self {
273+
// ListNode {
274+
// next: None,
275+
// val
276+
// }
277+
// }
278+
// }
279+
// Definition for a binary tree node.
280+
// #[derive(Debug, PartialEq, Eq)]
281+
// pub struct TreeNode {
282+
// pub val: i32,
283+
// pub left: Option<Rc<RefCell<TreeNode>>>,
284+
// pub right: Option<Rc<RefCell<TreeNode>>>,
285+
// }
286+
//
287+
// impl TreeNode {
288+
// #[inline]
289+
// pub fn new(val: i32) -> Self {
290+
// TreeNode {
291+
// val,
292+
// left: None,
293+
// right: None
294+
// }
295+
// }
296+
// }
297+
use std::rc::Rc;
298+
use std::cell::RefCell;
299+
impl Solution {
300+
fn build(vals: &Vec<i32>, start: usize, end: usize) -> Option<Rc<RefCell<TreeNode>>> {
301+
if (start == end) {
302+
return None;
303+
}
304+
let mid = (start + end) >> 1;
305+
Some(Rc::new(RefCell::new(TreeNode {
306+
val: vals[mid],
307+
left: Self::build(vals, start, mid),
308+
right: Self::build(vals, mid + 1, end),
309+
})))
310+
}
311+
312+
pub fn sorted_list_to_bst(head: Option<Box<ListNode>>) -> Option<Rc<RefCell<TreeNode>>> {
313+
let mut vals = Vec::new();
314+
let mut cur = &head;
315+
while let Some(node) = cur {
316+
vals.push(node.val);
317+
cur = &node.next;
318+
}
319+
Self::build(&vals, 0, vals.len())
320+
}
321+
}
322+
```
323+
324+
### **C**
325+
326+
```c
327+
/**
328+
* Definition for singly-linked list.
329+
* struct ListNode {
330+
* int val;
331+
* struct ListNode *next;
332+
* };
333+
*/
334+
/**
335+
* Definition for a binary tree node.
336+
* struct TreeNode {
337+
* int val;
338+
* struct TreeNode *left;
339+
* struct TreeNode *right;
340+
* };
341+
*/
342+
struct ListNode *find(struct ListNode *start, struct ListNode *end) {
343+
struct ListNode *fast = start;
344+
struct ListNode *slow = start;
345+
while (fast != end && fast->next != end) {
346+
fast = fast->next->next;
347+
slow = slow->next;
348+
}
349+
return slow;
350+
}
351+
352+
struct TreeNode *bulid(struct ListNode *start, struct ListNode *end) {
353+
if (start == end) {
354+
return NULL;
355+
}
356+
struct ListNode *node = find(start, end);
357+
struct TreeNode *ans = malloc(sizeof(struct TreeNode));
358+
ans->val = node->val;
359+
ans->left = bulid(start, node);
360+
ans->right = bulid(node->next, end);
361+
return ans;
362+
}
363+
364+
struct TreeNode *sortedListToBST(struct ListNode *head) {
365+
return bulid(head, NULL);
366+
}
367+
```
368+
208369
### **...**
209370
210371
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* struct TreeNode *left;
13+
* struct TreeNode *right;
14+
* };
15+
*/
16+
struct ListNode *find(struct ListNode *start, struct ListNode *end) {
17+
struct ListNode *fast = start;
18+
struct ListNode *slow = start;
19+
while (fast != end && fast->next != end) {
20+
fast = fast->next->next;
21+
slow = slow->next;
22+
}
23+
return slow;
24+
}
25+
26+
struct TreeNode *bulid(struct ListNode *start, struct ListNode *end) {
27+
if (start == end) {
28+
return NULL;
29+
}
30+
struct ListNode *node = find(start, end);
31+
struct TreeNode *ans = malloc(sizeof(struct TreeNode));
32+
ans->val = node->val;
33+
ans->left = bulid(start, node);
34+
ans->right = bulid(node->next, end);
35+
return ans;
36+
}
37+
38+
struct TreeNode *sortedListToBST(struct ListNode *head) {
39+
return bulid(head, NULL);
40+
}

0 commit comments

Comments
 (0)