题目:leetcode94. 二叉树的中序遍历(递归实现)
描述:
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
思路:使用递归算法,首先中序遍历左子树,然后输出当前结点的值,然后再遍历右子树。
import java.util.ArrayList;
import java.util.List;
public class Solution {
public List<Integer> inorderTraversal(TreeNode root)
{
if(root==null)
return new ArrayList<>();
List<Integer> result=new ArrayList<>();
inorder(root,result);
return result;
}
private void inorder(TreeNode root, List<Integer> result) {
if(root==null)
return ;
inorder(root.left,result);
result.add(root.val);
inorder(root.right,result);
}
}