题目
题目链接
写个简单的爽爽
题解
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
recursion(res, root);
return res;
}
private void recursion(List<Integer> res, TreeNode node) {
if (node == null) {
return;
}
if (node.left != null) {
recursion(res, node.left);
}
res.add(node.val);
if (node.right != null) {
recursion(res, node.right);
}
}
}