要求:二叉树中序遍历。中序遍历:左中右
代码:
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result=new ArrayList<>();
if(root!=null){
inorder(result,root);
}
return result;
}
private void inorder(List<Integer> list,TreeNode root){
if(root.left!=null){
inorder(list,root.left);
}
list.add(root.val);
if(root.right!=null){
inorder(list,root.right);
}
}