-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.java
More file actions
50 lines (44 loc) · 1001 Bytes
/
BinaryTreeInorderTraversal.java
File metadata and controls
50 lines (44 loc) · 1001 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
40
41
42
43
44
45
46
47
48
49
50
package binary_tree;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Wenhang Chen
* @Description:给定一个二叉树,返回它的中序 遍历。 示例:
* <p>
* 输入: [1,null,2,3]
* 1
* \
* 2
* /
* 3
* <p>
* 输出: [1,3,2]
* @Date: Created in 19:25 1/21/2020
* @Modified by:
*/
public class BinaryTreeInorderTraversal {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
helper(root, res);
return res;
}
public void helper(TreeNode root, List<Integer> res) {
if (root != null) {
if (root.left != null) {
helper(root.left, res);
}
res.add(root.val);
if (root.right != null) {
helper(root.right, res);
}
}
}
}