Skip to content

Commit 3ef01ae

Browse files
committed
feat: add solutions to lc problem: No.0173
No.0173.Binary Search Tree Iterator
1 parent c408386 commit 3ef01ae

File tree

4 files changed

+610
-0
lines changed

4 files changed

+610
-0
lines changed

solution/0100-0199/0173.Binary Search Tree Iterator/README.md

+246
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,252 @@ BSTIterator.prototype.hasNext = function () {
440440
*/
441441
```
442442

443+
### **TypeScript**
444+
445+
```ts
446+
/**
447+
* Definition for a binary tree node.
448+
* class TreeNode {
449+
* val: number
450+
* left: TreeNode | null
451+
* right: TreeNode | null
452+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
453+
* this.val = (val===undefined ? 0 : val)
454+
* this.left = (left===undefined ? null : left)
455+
* this.right = (right===undefined ? null : right)
456+
* }
457+
* }
458+
*/
459+
460+
class BSTIterator {
461+
private data: number[];
462+
private index: number;
463+
464+
constructor(root: TreeNode | null) {
465+
this.index = 0;
466+
this.data = [];
467+
const dfs = (root: TreeNode | null) => {
468+
if (root == null) {
469+
return;
470+
}
471+
const { val, left, right } = root;
472+
dfs(left);
473+
this.data.push(val);
474+
dfs(right);
475+
};
476+
dfs(root);
477+
}
478+
479+
next(): number {
480+
return this.data[this.index++];
481+
}
482+
483+
hasNext(): boolean {
484+
return this.index < this.data.length;
485+
}
486+
}
487+
488+
/**
489+
* Your BSTIterator object will be instantiated and called as such:
490+
* var obj = new BSTIterator(root)
491+
* var param_1 = obj.next()
492+
* var param_2 = obj.hasNext()
493+
*/
494+
```
495+
496+
```ts
497+
/**
498+
* Definition for a binary tree node.
499+
* class TreeNode {
500+
* val: number
501+
* left: TreeNode | null
502+
* right: TreeNode | null
503+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
504+
* this.val = (val===undefined ? 0 : val)
505+
* this.left = (left===undefined ? null : left)
506+
* this.right = (right===undefined ? null : right)
507+
* }
508+
* }
509+
*/
510+
511+
class BSTIterator {
512+
private stack: TreeNode[];
513+
514+
constructor(root: TreeNode | null) {
515+
this.stack = [];
516+
const dfs = (root: TreeNode | null) => {
517+
if (root == null) {
518+
return;
519+
}
520+
this.stack.push(root);
521+
dfs(root.left);
522+
};
523+
dfs(root);
524+
}
525+
526+
next(): number {
527+
const { val, right } = this.stack.pop();
528+
if (right) {
529+
let cur = right;
530+
while (cur != null) {
531+
this.stack.push(cur);
532+
cur = cur.left;
533+
}
534+
}
535+
return val;
536+
}
537+
538+
hasNext(): boolean {
539+
return this.stack.length !== 0;
540+
}
541+
}
542+
543+
/**
544+
* Your BSTIterator object will be instantiated and called as such:
545+
* var obj = new BSTIterator(root)
546+
* var param_1 = obj.next()
547+
* var param_2 = obj.hasNext()
548+
*/
549+
```
550+
551+
### **Rust**
552+
553+
```rust
554+
// Definition for a binary tree node.
555+
// #[derive(Debug, PartialEq, Eq)]
556+
// pub struct TreeNode {
557+
// pub val: i32,
558+
// pub left: Option<Rc<RefCell<TreeNode>>>,
559+
// pub right: Option<Rc<RefCell<TreeNode>>>,
560+
// }
561+
//
562+
// impl TreeNode {
563+
// #[inline]
564+
// pub fn new(val: i32) -> Self {
565+
// TreeNode {
566+
// val,
567+
// left: None,
568+
// right: None
569+
// }
570+
// }
571+
// }
572+
struct BSTIterator {
573+
vals: Vec<i32>,
574+
index: usize,
575+
}
576+
577+
use std::rc::Rc;
578+
use std::cell::RefCell;
579+
/**
580+
* `&self` means the method takes an immutable reference.
581+
* If you need a mutable reference, change it to `&mut self` instead.
582+
*/
583+
impl BSTIterator {
584+
fn inorder(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
585+
if let Some(node) = root {
586+
let node = node.as_ref().borrow();
587+
Self::inorder(&node.left, res);
588+
res.push(node.val);
589+
Self::inorder(&node.right, res);
590+
}
591+
}
592+
593+
fn new(root: Option<Rc<RefCell<TreeNode>>>) -> Self {
594+
let mut vals = vec![];
595+
Self::inorder(&root, &mut vals);
596+
BSTIterator {
597+
vals,
598+
index: 0,
599+
}
600+
}
601+
602+
fn next(&mut self) -> i32 {
603+
self.index += 1;
604+
self.vals[self.index - 1]
605+
}
606+
607+
fn has_next(&self) -> bool {
608+
self.index != self.vals.len()
609+
}
610+
}
611+
612+
/**
613+
* Your BSTIterator object will be instantiated and called as such:
614+
* let obj = BSTIterator::new(root);
615+
* let ret_1: i32 = obj.next();
616+
* let ret_2: bool = obj.has_next();
617+
*/
618+
```
619+
620+
```rust
621+
// Definition for a binary tree node.
622+
// #[derive(Debug, PartialEq, Eq)]
623+
// pub struct TreeNode {
624+
// pub val: i32,
625+
// pub left: Option<Rc<RefCell<TreeNode>>>,
626+
// pub right: Option<Rc<RefCell<TreeNode>>>,
627+
// }
628+
//
629+
// impl TreeNode {
630+
// #[inline]
631+
// pub fn new(val: i32) -> Self {
632+
// TreeNode {
633+
// val,
634+
// left: None,
635+
// right: None
636+
// }
637+
// }
638+
// }
639+
struct BSTIterator {
640+
stack: Vec<Option<Rc<RefCell<TreeNode>>>>,
641+
}
642+
643+
use std::rc::Rc;
644+
use std::cell::RefCell;
645+
/**
646+
* `&self` means the method takes an immutable reference.
647+
* If you need a mutable reference, change it to `&mut self` instead.
648+
*/
649+
impl BSTIterator {
650+
fn dfs(
651+
mut root: Option<Rc<RefCell<TreeNode>>>,
652+
stack: &mut Vec<Option<Rc<RefCell<TreeNode>>>>,
653+
) {
654+
if root.is_some() {
655+
let left = root.as_mut().unwrap().borrow_mut().left.take();
656+
stack.push(root);
657+
Self::dfs(left, stack);
658+
}
659+
}
660+
661+
fn new(root: Option<Rc<RefCell<TreeNode>>>) -> Self {
662+
let mut stack = vec![];
663+
Self::dfs(root, &mut stack);
664+
BSTIterator { stack }
665+
}
666+
667+
fn next(&mut self) -> i32 {
668+
let node = self.stack.pop().unwrap().unwrap();
669+
let mut node = node.borrow_mut();
670+
if node.right.is_some() {
671+
Self::dfs(node.right.take(), &mut self.stack)
672+
}
673+
node.val
674+
}
675+
676+
fn has_next(&self) -> bool {
677+
self.stack.len() != 0
678+
}
679+
}
680+
681+
/**
682+
* Your BSTIterator object will be instantiated and called as such:
683+
* let obj = BSTIterator::new(root);
684+
* let ret_1: i32 = obj.next();
685+
* let ret_2: bool = obj.has_next();
686+
*/
687+
```
688+
443689
### **...**
444690

445691
```

0 commit comments

Comments
 (0)