Skip to content

Commit 10a7019

Browse files
committed
feat: add solutions to lc problem: No.0094
No.0094.Binary Tree Inorder Traversal
1 parent fcf836f commit 10a7019

File tree

4 files changed

+453
-0
lines changed

4 files changed

+453
-0
lines changed

solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md

+196
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,202 @@ func inorderTraversal(root *TreeNode) []int {
482482
}
483483
```
484484

485+
### **TypeScript**
486+
487+
递归:
488+
489+
```ts
490+
/**
491+
* Definition for a binary tree node.
492+
* class TreeNode {
493+
* val: number
494+
* left: TreeNode | null
495+
* right: TreeNode | null
496+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
497+
* this.val = (val===undefined ? 0 : val)
498+
* this.left = (left===undefined ? null : left)
499+
* this.right = (right===undefined ? null : right)
500+
* }
501+
* }
502+
*/
503+
504+
function inorderTraversal(root: TreeNode | null): number[] {
505+
if (root == null) {
506+
return [];
507+
}
508+
return [
509+
...inorderTraversal(root.left),
510+
root.val,
511+
...inorderTraversal(root.right),
512+
];
513+
}
514+
```
515+
516+
迭代:
517+
518+
```ts
519+
/**
520+
* Definition for a binary tree node.
521+
* class TreeNode {
522+
* val: number
523+
* left: TreeNode | null
524+
* right: TreeNode | null
525+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
526+
* this.val = (val===undefined ? 0 : val)
527+
* this.left = (left===undefined ? null : left)
528+
* this.right = (right===undefined ? null : right)
529+
* }
530+
* }
531+
*/
532+
533+
function inorderTraversal(root: TreeNode | null): number[] {
534+
const res = [];
535+
const stack = [];
536+
while (root != null || stack.length != 0) {
537+
if (root != null) {
538+
stack.push(root);
539+
root = root.left;
540+
} else {
541+
const { val, right } = stack.pop();
542+
res.push(val);
543+
root = right;
544+
}
545+
}
546+
return res;
547+
}
548+
```
549+
550+
Morris 遍历:
551+
552+
```ts
553+
/**
554+
* Definition for a binary tree node.
555+
* class TreeNode {
556+
* val: number
557+
* left: TreeNode | null
558+
* right: TreeNode | null
559+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
560+
* this.val = (val===undefined ? 0 : val)
561+
* this.left = (left===undefined ? null : left)
562+
* this.right = (right===undefined ? null : right)
563+
* }
564+
* }
565+
*/
566+
567+
function inorderTraversal(root: TreeNode | null): number[] {
568+
const res = [];
569+
while (root != null) {
570+
const { val, left, right } = root;
571+
if (left == null) {
572+
res.push(val);
573+
root = right;
574+
} else {
575+
let mostRight = left;
576+
while (mostRight.right != null && mostRight.right != root) {
577+
mostRight = mostRight.right;
578+
}
579+
if (mostRight.right == root) {
580+
res.push(val);
581+
mostRight.right = null;
582+
root = right;
583+
} else {
584+
mostRight.right = root;
585+
root = left;
586+
}
587+
}
588+
}
589+
return res;
590+
}
591+
```
592+
593+
### **Rust**
594+
595+
递归:
596+
597+
```rust
598+
// Definition for a binary tree node.
599+
// #[derive(Debug, PartialEq, Eq)]
600+
// pub struct TreeNode {
601+
// pub val: i32,
602+
// pub left: Option<Rc<RefCell<TreeNode>>>,
603+
// pub right: Option<Rc<RefCell<TreeNode>>>,
604+
// }
605+
//
606+
// impl TreeNode {
607+
// #[inline]
608+
// pub fn new(val: i32) -> Self {
609+
// TreeNode {
610+
// val,
611+
// left: None,
612+
// right: None
613+
// }
614+
// }
615+
// }
616+
use std::rc::Rc;
617+
use std::cell::RefCell;
618+
impl Solution {
619+
fn dfs (root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
620+
if root.is_none() {
621+
return;
622+
}
623+
let node = root.as_ref().unwrap().borrow();
624+
Self::dfs(&node.left, res);
625+
res.push(node.val);
626+
Self::dfs(&node.right, res);
627+
}
628+
629+
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
630+
let mut res = vec![];
631+
Self::dfs(&root, &mut res);
632+
res
633+
}
634+
}
635+
```
636+
637+
迭代:
638+
639+
```rust
640+
// Definition for a binary tree node.
641+
// #[derive(Debug, PartialEq, Eq)]
642+
// pub struct TreeNode {
643+
// pub val: i32,
644+
// pub left: Option<Rc<RefCell<TreeNode>>>,
645+
// pub right: Option<Rc<RefCell<TreeNode>>>,
646+
// }
647+
//
648+
// impl TreeNode {
649+
// #[inline]
650+
// pub fn new(val: i32) -> Self {
651+
// TreeNode {
652+
// val,
653+
// left: None,
654+
// right: None
655+
// }
656+
// }
657+
// }
658+
use std::rc::Rc;
659+
use std::cell::RefCell;
660+
impl Solution {
661+
pub fn inorder_traversal(mut root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
662+
let mut res = vec![];
663+
let mut stack = vec![];
664+
while root.is_some() || !stack.is_empty() {
665+
if root.is_some() {
666+
let next = root.as_mut().unwrap().borrow_mut().left.take();
667+
stack.push(root);
668+
root = next;
669+
} else {
670+
let mut node = stack.pop().unwrap();
671+
let mut node = node.as_mut().unwrap().borrow_mut();
672+
res.push(node.val);
673+
root = node.right.take();
674+
}
675+
}
676+
res
677+
}
678+
}
679+
```
680+
485681
### **...**
486682

487683
```

0 commit comments

Comments
 (0)