Skip to content

Commit a3507f7

Browse files
authored
feat: add solutions to lc problem: No.0094 (doocs#2333)
No.0094.Binary Tree Inorder Traversal
1 parent 574af9d commit a3507f7

17 files changed

+616
-271
lines changed

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

+210-91
Large diffs are not rendered by default.

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

+209-90
Large diffs are not rendered by default.

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.cpp

+8-18
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,15 @@ class Solution {
1313
public:
1414
vector<int> inorderTraversal(TreeNode* root) {
1515
vector<int> ans;
16-
while (root) {
17-
if (!root->left) {
18-
ans.push_back(root->val);
19-
root = root->right;
20-
} else {
21-
TreeNode* prev = root->left;
22-
while (prev->right && prev->right != root) {
23-
prev = prev->right;
24-
}
25-
if (!prev->right) {
26-
prev->right = root;
27-
root = root->left;
28-
} else {
29-
ans.push_back(root->val);
30-
prev->right = nullptr;
31-
root = root->right;
32-
}
16+
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
17+
if (!root) {
18+
return;
3319
}
34-
}
20+
dfs(root->left);
21+
ans.push_back(root->val);
22+
dfs(root->right);
23+
};
24+
dfs(root);
3525
return ans;
3626
}
3727
};

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.go

+10-20
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,16 @@
66
* Right *TreeNode
77
* }
88
*/
9-
func inorderTraversal(root *TreeNode) []int {
10-
var ans []int
11-
for root != nil {
12-
if root.Left == nil {
13-
ans = append(ans, root.Val)
14-
root = root.Right
15-
} else {
16-
prev := root.Left
17-
for prev.Right != nil && prev.Right != root {
18-
prev = prev.Right
19-
}
20-
if prev.Right == nil {
21-
prev.Right = root
22-
root = root.Left
23-
} else {
24-
ans = append(ans, root.Val)
25-
prev.Right = nil
26-
root = root.Right
27-
}
9+
func inorderTraversal(root *TreeNode) (ans []int) {
10+
var dfs func(*TreeNode)
11+
dfs = func(root *TreeNode) {
12+
if root == nil {
13+
return
2814
}
15+
dfs(root.Left)
16+
ans = append(ans, root.Val)
17+
dfs(root.Right)
2918
}
30-
return ans
19+
dfs(root)
20+
return
3121
}

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
* }
1515
*/
1616
class Solution {
17-
private List<Integer> ans;
17+
private List<Integer> ans = new ArrayList<>();
1818

1919
public List<Integer> inorderTraversal(TreeNode root) {
20-
ans = new ArrayList<>();
2120
dfs(root);
2221
return ans;
2322
}

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
* @return {number[]}
1212
*/
1313
var inorderTraversal = function (root) {
14-
let ans = [];
15-
function dfs(root) {
16-
if (!root) return;
14+
const ans = [];
15+
const dfs = root => {
16+
if (!root) {
17+
return;
18+
}
1719
dfs(root.left);
1820
ans.push(root.val);
1921
dfs(root.right);
20-
}
22+
};
2123
dfs(root);
2224
return ans;
2325
};

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
use std::rc::Rc;
2020
use std::cell::RefCell;
2121
impl Solution {
22-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
22+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, ans: &mut Vec<i32>) {
2323
if root.is_none() {
2424
return;
2525
}
2626
let node = root.as_ref().unwrap().borrow();
27-
Self::dfs(&node.left, res);
28-
res.push(node.val);
29-
Self::dfs(&node.right, res);
27+
Self::dfs(&node.left, ans);
28+
ans.push(node.val);
29+
Self::dfs(&node.right, ans);
3030
}
3131

3232
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
33-
let mut res = vec![];
34-
Self::dfs(&root, &mut res);
35-
res
33+
let mut ans = vec![];
34+
Self::dfs(&root, &mut ans);
35+
ans
3636
}
3737
}

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
*/
1414

1515
function inorderTraversal(root: TreeNode | null): number[] {
16-
if (root == null) {
17-
return [];
18-
}
19-
return [...inorderTraversal(root.left), root.val, ...inorderTraversal(root.right)];
16+
const ans: number[] = [];
17+
const dfs = (root: TreeNode | null) => {
18+
if (!root) {
19+
return;
20+
}
21+
dfs(root.left);
22+
ans.push(root.val);
23+
dfs(root.right);
24+
};
25+
dfs(root);
26+
return ans;
2027
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
vector<int> inorderTraversal(TreeNode* root) {
15+
vector<int> ans;
16+
stack<TreeNode*> stk;
17+
while (root || stk.size()) {
18+
if (root) {
19+
stk.push(root);
20+
root = root->left;
21+
} else {
22+
root = stk.top();
23+
stk.pop();
24+
ans.push_back(root->val);
25+
root = root->right;
26+
}
27+
}
28+
return ans;
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func inorderTraversal(root *TreeNode) (ans []int) {
10+
stk := []*TreeNode{}
11+
for root != nil || len(stk) > 0 {
12+
if root != nil {
13+
stk = append(stk, root)
14+
root = root.Left
15+
} else {
16+
root = stk[len(stk)-1]
17+
stk = stk[:len(stk)-1]
18+
ans = append(ans, root.Val)
19+
root = root.Right
20+
}
21+
}
22+
return
23+
}

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* @return {number[]}
1212
*/
1313
var inorderTraversal = function (root) {
14-
let ans = [],
15-
stk = [];
14+
const stk = [];
15+
const ans = [];
1616
while (root || stk.length > 0) {
1717
if (root) {
1818
stk.push(root);

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution2.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ use std::rc::Rc;
2020
use std::cell::RefCell;
2121
impl Solution {
2222
pub fn inorder_traversal(mut root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
23-
let mut res = vec![];
24-
let mut stack = vec![];
25-
while root.is_some() || !stack.is_empty() {
23+
let mut ans = vec![];
24+
let mut stk = vec![];
25+
while root.is_some() || !stk.is_empty() {
2626
if root.is_some() {
2727
let next = root.as_mut().unwrap().borrow_mut().left.take();
28-
stack.push(root);
28+
stk.push(root);
2929
root = next;
3030
} else {
31-
let mut node = stack.pop().unwrap();
31+
let mut node = stk.pop().unwrap();
3232
let mut node = node.as_mut().unwrap().borrow_mut();
33-
res.push(node.val);
33+
ans.push(node.val);
3434
root = node.right.take();
3535
}
3636
}
37-
res
37+
ans
3838
}
3939
}

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution2.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
*/
1414

1515
function inorderTraversal(root: TreeNode | null): number[] {
16-
const res = [];
17-
const stack = [];
18-
while (root != null || stack.length != 0) {
19-
if (root != null) {
20-
stack.push(root);
16+
const stk: TreeNode[] = [];
17+
const ans: number[] = [];
18+
while (root || stk.length > 0) {
19+
if (root) {
20+
stk.push(root);
2121
root = root.left;
2222
} else {
23-
const { val, right } = stack.pop();
24-
res.push(val);
25-
root = right;
23+
root = stk.pop();
24+
ans.push(root.val);
25+
root = root.right;
2626
}
2727
}
28-
return res;
28+
return ans;
2929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
vector<int> inorderTraversal(TreeNode* root) {
15+
vector<int> ans;
16+
while (root) {
17+
if (!root->left) {
18+
ans.push_back(root->val);
19+
root = root->right;
20+
} else {
21+
TreeNode* prev = root->left;
22+
while (prev->right && prev->right != root) {
23+
prev = prev->right;
24+
}
25+
if (!prev->right) {
26+
prev->right = root;
27+
root = root->left;
28+
} else {
29+
ans.push_back(root->val);
30+
prev->right = nullptr;
31+
root = root->right;
32+
}
33+
}
34+
}
35+
return ans;
36+
}
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func inorderTraversal(root *TreeNode) (ans []int) {
10+
for root != nil {
11+
if root.Left == nil {
12+
ans = append(ans, root.Val)
13+
root = root.Right
14+
} else {
15+
prev := root.Left
16+
for prev.Right != nil && prev.Right != root {
17+
prev = prev.Right
18+
}
19+
if prev.Right == nil {
20+
prev.Right = root
21+
root = root.Left
22+
} else {
23+
ans = append(ans, root.Val)
24+
prev.Right = nil
25+
root = root.Right
26+
}
27+
}
28+
}
29+
return
30+
}

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @return {number[]}
1212
*/
1313
var inorderTraversal = function (root) {
14-
let ans = [];
14+
const ans = [];
1515
while (root) {
1616
if (!root.left) {
1717
ans.push(root.val);

solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution3.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,25 @@
1313
*/
1414

1515
function inorderTraversal(root: TreeNode | null): number[] {
16-
const res = [];
17-
while (root != null) {
18-
const { val, left, right } = root;
19-
if (left == null) {
20-
res.push(val);
21-
root = right;
16+
const ans: number[] = [];
17+
while (root) {
18+
if (!root.left) {
19+
ans.push(root.val);
20+
root = root.right;
2221
} else {
23-
let mostRight = left;
24-
while (mostRight.right != null && mostRight.right != root) {
25-
mostRight = mostRight.right;
22+
let prev = root.left;
23+
while (prev.right && prev.right != root) {
24+
prev = prev.right;
2625
}
27-
if (mostRight.right == root) {
28-
res.push(val);
29-
mostRight.right = null;
30-
root = right;
26+
if (!prev.right) {
27+
prev.right = root;
28+
root = root.left;
3129
} else {
32-
mostRight.right = root;
33-
root = left;
30+
ans.push(root.val);
31+
prev.right = null;
32+
root = root.right;
3433
}
3534
}
3635
}
37-
return res;
36+
return ans;
3837
}

0 commit comments

Comments
 (0)