Skip to content

Commit 11df4a7

Browse files
committed
Add solution 094
1 parent 2fd42d5 commit 11df4a7

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Complete solutions to Leetcode problems, updated daily.
4848
| 063 | [Unique Paths II](https://github.com/yanglbme/leetcode/tree/master/solution/063.Unique%20Paths%20II) | `Array`, `Dynamic Programming` |
4949
| 075 | [Sort Colors](https://github.com/yanglbme/leetcode/tree/master/solution/075.Sort%20Colors) | `Array`, `Two Pointers`, `Sort` |
5050
| 082 | [Remove Duplicates from Sorted List II](https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II) | `Linked List` |
51+
| 094 | [Binary Tree Inorder Traversal](https://github.com/yanglbme/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal) | `Hash Table`, `Stack`, `Tree` |
5152
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/yanglbme/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
5253

5354

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## 二叉树的中序遍历
2+
### 题目描述
3+
4+
给定一个二叉树,返回它的中序 遍历。
5+
6+
示例:
7+
```
8+
输入: [1,null,2,3]
9+
1
10+
\
11+
2
12+
/
13+
3
14+
15+
输出: [1,3,2]
16+
```
17+
18+
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
19+
20+
### 解法
21+
递归算法,先递归左子树,再访问根结点,最后递归右子树。
22+
23+
- 递归算法
24+
25+
```java
26+
27+
/**
28+
* Definition for a binary tree node.
29+
* public class TreeNode {
30+
* int val;
31+
* TreeNode left;
32+
* TreeNode right;
33+
* TreeNode(int x) { val = x; }
34+
* }
35+
*/
36+
class Solution {
37+
public List<Integer> inorderTraversal(TreeNode root) {
38+
List<Integer> list = new ArrayList<>();
39+
inorderTraversal(root, list);
40+
return list;
41+
}
42+
43+
private void inorderTraversal(TreeNode root, List<Integer> list) {
44+
if (root == null) {
45+
return;
46+
}
47+
inorderTraversal(root.left, list);
48+
list.add(root.val);
49+
inorderTraversal(root.right, list);
50+
}
51+
52+
}
53+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<Integer> inorderTraversal(TreeNode root) {
3+
List<Integer> list = new ArrayList<>();
4+
inorderTraversal(root, list);
5+
return list;
6+
}
7+
8+
private void inorderTraversal(TreeNode root, List<Integer> list) {
9+
if (root == null) {
10+
return;
11+
}
12+
inorderTraversal(root.left, list);
13+
list.add(root.val);
14+
inorderTraversal(root.right, list);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)