-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSolution94.java
More file actions
39 lines (32 loc) · 869 Bytes
/
Solution94.java
File metadata and controls
39 lines (32 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package stack_problem;
import java.util.ArrayList;
import java.util.List;
/**
* O(n):树中的节点个数。
* O(h):树的高度。
*/
public class Solution94 {
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public List<Integer> inorderTraversal(TreeNode root) {
// 1、创建一个返回列表
List<Integer> res = new ArrayList<>();
inorderTraversal(root, res);
return res;
}
// 2、递归实现中序遍历:左根右
private void inorderTraversal(TreeNode root, List<Integer> res) {
if (root != null) {
inorderTraversal(root.left, res);
res.add(root.val);
inorderTraversal(root.right, res);
}
}
}