Skip to content

Commit af2646d

Browse files
committed
feat: add solutions to lcof2 problems: No.052, 055
- No.剑指 Offer II 052. 展平二叉搜索树 - No.剑指 Offer II 055. 二叉搜索树迭代器
1 parent 9f79946 commit af2646d

File tree

7 files changed

+443
-0
lines changed

7 files changed

+443
-0
lines changed

lcof2/剑指 Offer II 052. 展平二叉搜索树/README.md

+118
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,124 @@ public:
199199
};
200200
```
201201
202+
### **TypeScript**
203+
204+
```ts
205+
/**
206+
* Definition for a binary tree node.
207+
* class TreeNode {
208+
* val: number
209+
* left: TreeNode | null
210+
* right: TreeNode | null
211+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
212+
* this.val = (val===undefined ? 0 : val)
213+
* this.left = (left===undefined ? null : left)
214+
* this.right = (right===undefined ? null : right)
215+
* }
216+
* }
217+
*/
218+
219+
function increasingBST(root: TreeNode | null): TreeNode | null {
220+
const dummy = new TreeNode();
221+
let cur = dummy;
222+
const dfs = (root: TreeNode | null) => {
223+
if (root == null) {
224+
return;
225+
}
226+
dfs(root.left);
227+
cur.right = new TreeNode(root.val);
228+
cur = cur.right;
229+
dfs(root.right);
230+
};
231+
dfs(root);
232+
return dummy.right;
233+
}
234+
```
235+
236+
### **Rust**
237+
238+
```rust
239+
// Definition for a binary tree node.
240+
// #[derive(Debug, PartialEq, Eq)]
241+
// pub struct TreeNode {
242+
// pub val: i32,
243+
// pub left: Option<Rc<RefCell<TreeNode>>>,
244+
// pub right: Option<Rc<RefCell<TreeNode>>>,
245+
// }
246+
//
247+
// impl TreeNode {
248+
// #[inline]
249+
// pub fn new(val: i32) -> Self {
250+
// TreeNode {
251+
// val,
252+
// left: None,
253+
// right: None
254+
// }
255+
// }
256+
// }
257+
use std::rc::Rc;
258+
use std::cell::RefCell;
259+
impl Solution {
260+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, vals: &mut Vec<i32>) {
261+
if root.is_none() {
262+
return;
263+
}
264+
let node = root.as_ref().unwrap().borrow();
265+
Self::dfs(&node.left, vals);
266+
vals.push(node.val);
267+
Self::dfs(&node.right, vals);
268+
}
269+
270+
pub fn increasing_bst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
271+
let mut vals = Vec::new();
272+
Self::dfs(&root, &mut vals);
273+
let mut dummy = Rc::new(RefCell::new(TreeNode::new(0)));
274+
for &val in vals.iter().rev() {
275+
let mut dummy = dummy.as_ref().borrow_mut();
276+
dummy.right = Some(Rc::new(RefCell::new(TreeNode {
277+
val,
278+
left: None,
279+
right: dummy.right.take(),
280+
})));
281+
}
282+
let ans = dummy.as_ref().borrow_mut().right.take();
283+
ans
284+
}
285+
}
286+
```
287+
288+
### **C**
289+
290+
```c
291+
/**
292+
* Definition for a binary tree node.
293+
* struct TreeNode {
294+
* int val;
295+
* struct TreeNode *left;
296+
* struct TreeNode *right;
297+
* };
298+
*/
299+
300+
struct TreeNode *dfs(struct TreeNode *root, struct TreeNode *cur) {
301+
if (!root) {
302+
return cur;
303+
}
304+
cur = dfs(root->left, cur);
305+
cur->right = malloc(sizeof(struct TreeNode));
306+
cur->right->val = root->val;
307+
cur->right->left = NULL;
308+
cur->right->right = NULL;
309+
cur = cur->right;
310+
return dfs(root->right, cur);
311+
}
312+
313+
struct TreeNode *increasingBST(struct TreeNode *root) {
314+
struct TreeNode *dummy = malloc(sizeof(struct TreeNode));
315+
dfs(root, dummy);
316+
return dummy->right;
317+
}
318+
```
319+
202320
### **...**
203321
204322
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
struct TreeNode *dfs(struct TreeNode *root, struct TreeNode *cur) {
11+
if (!root) {
12+
return cur;
13+
}
14+
cur = dfs(root->left, cur);
15+
cur->right = malloc(sizeof(struct TreeNode));
16+
cur->right->val = root->val;
17+
cur->right->left = NULL;
18+
cur->right->right = NULL;
19+
cur = cur->right;
20+
return dfs(root->right, cur);
21+
}
22+
23+
struct TreeNode *increasingBST(struct TreeNode *root) {
24+
struct TreeNode *dummy = malloc(sizeof(struct TreeNode));
25+
dfs(root, dummy);
26+
return dummy->right;
27+
}
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+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, vals: &mut Vec<i32>) {
23+
if root.is_none() {
24+
return;
25+
}
26+
let node = root.as_ref().unwrap().borrow();
27+
Self::dfs(&node.left, vals);
28+
vals.push(node.val);
29+
Self::dfs(&node.right, vals);
30+
}
31+
32+
pub fn increasing_bst(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
33+
let mut vals = Vec::new();
34+
Self::dfs(&root, &mut vals);
35+
let mut dummy = Rc::new(RefCell::new(TreeNode::new(0)));
36+
for &val in vals.iter().rev() {
37+
let mut dummy = dummy.as_ref().borrow_mut();
38+
dummy.right = Some(Rc::new(RefCell::new(TreeNode {
39+
val,
40+
left: None,
41+
right: dummy.right.take(),
42+
})));
43+
}
44+
let ans = dummy.as_ref().borrow_mut().right.take();
45+
ans
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 increasingBST(root: TreeNode | null): TreeNode | null {
16+
const dummy = new TreeNode();
17+
let cur = dummy;
18+
const dfs = (root: TreeNode | null) => {
19+
if (root == null) {
20+
return;
21+
}
22+
dfs(root.left);
23+
cur.right = new TreeNode(root.val);
24+
cur = cur.right;
25+
dfs(root.right);
26+
};
27+
dfs(root);
28+
return dummy.right;
29+
}

lcof2/剑指 Offer II 055. 二叉搜索树迭代器/README.md

+116
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,122 @@ BSTIterator.prototype.hasNext = function () {
442442
*/
443443
```
444444

445+
### **TypeScript**
446+
447+
```ts
448+
/**
449+
* Definition for a binary tree node.
450+
* class TreeNode {
451+
* val: number
452+
* left: TreeNode | null
453+
* right: TreeNode | null
454+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
455+
* this.val = (val===undefined ? 0 : val)
456+
* this.left = (left===undefined ? null : left)
457+
* this.right = (right===undefined ? null : right)
458+
* }
459+
* }
460+
*/
461+
462+
class BSTIterator {
463+
private stack: number[];
464+
465+
constructor(root: TreeNode | null) {
466+
this.stack = [];
467+
const dfs = ({ val, left, right }: TreeNode) => {
468+
right && dfs(right);
469+
this.stack.push(val);
470+
left && dfs(left);
471+
};
472+
dfs(root);
473+
}
474+
475+
next(): number {
476+
return this.stack.pop();
477+
}
478+
479+
hasNext(): boolean {
480+
return this.stack.length !== 0;
481+
}
482+
}
483+
484+
/**
485+
* Your BSTIterator object will be instantiated and called as such:
486+
* var obj = new BSTIterator(root)
487+
* var param_1 = obj.next()
488+
* var param_2 = obj.hasNext()
489+
*/
490+
```
491+
492+
### **Rust**
493+
494+
```rust
495+
// Definition for a binary tree node.
496+
// #[derive(Debug, PartialEq, Eq)]
497+
// pub struct TreeNode {
498+
// pub val: i32,
499+
// pub left: Option<Rc<RefCell<TreeNode>>>,
500+
// pub right: Option<Rc<RefCell<TreeNode>>>,
501+
// }
502+
//
503+
// impl TreeNode {
504+
// #[inline]
505+
// pub fn new(val: i32) -> Self {
506+
// TreeNode {
507+
// val,
508+
// left: None,
509+
// right: None
510+
// }
511+
// }
512+
// }
513+
use std::rc::Rc;
514+
use std::cell::RefCell;
515+
struct BSTIterator {
516+
stack: Vec<i32>
517+
}
518+
519+
520+
/**
521+
* `&self` means the method takes an immutable reference.
522+
* If you need a mutable reference, change it to `&mut self` instead.
523+
*/
524+
impl BSTIterator {
525+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, stack: &mut Vec<i32>) {
526+
let node = root.as_ref().unwrap().borrow();
527+
if node.right.is_some() {
528+
Self::dfs(&node.right, stack);
529+
}
530+
stack.push(node.val);
531+
if node.left.is_some() {
532+
Self::dfs(&node.left, stack);
533+
}
534+
}
535+
536+
fn new(root: Option<Rc<RefCell<TreeNode>>>) -> Self {
537+
let mut stack = Vec::new();
538+
Self::dfs(&root, &mut stack);
539+
Self {
540+
stack
541+
}
542+
}
543+
544+
fn next(&mut self) -> i32 {
545+
self.stack.pop().unwrap()
546+
}
547+
548+
fn has_next(&self) -> bool {
549+
!self.stack.is_empty()
550+
}
551+
}
552+
553+
/**
554+
* Your BSTIterator object will be instantiated and called as such:
555+
* let obj = BSTIterator::new(root);
556+
* let ret_1: i32 = obj.next();
557+
* let ret_2: bool = obj.has_next();
558+
*/
559+
```
560+
445561
### **...**
446562

447563
```

0 commit comments

Comments
 (0)