方法一:递归


/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    List<Integer> list=new ArrayList<>();
    public int[] inorderTraversal (TreeNode root) {
        // write code here
        inorder(root);
        int[] res=new int[list.size()];
        for(int i=0;i<list.size();i++){
            res[i]=list.get(i);
        }
        return res;
    }
    public void inorder(TreeNode root){
        if(root!=null){
            inorder(root.left);
            list.add(root.val);
            inorder(root.right);
        }
    }
}

方法二:栈


/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    List<Integer> list=new ArrayList<>();
    public int[] inorderTraversal (TreeNode root) {
        // write code here
        Stack<TreeNode> stack=new Stack<TreeNode>();
        TreeNode tempNode=root;
        while(tempNode!=null||!stack.isEmpty()){
            //处理左子树,入栈
            while(tempNode!=null){
                stack.push(tempNode);
                tempNode=tempNode.left;
            }
            //处理当前结点
            tempNode=stack.pop();
            list.add(tempNode.val);
            //处理右子树,若右子树为空tempNode在下一回合就不会将新结点入栈,将会从栈中取出上一个结点的根结点
            tempNode=tempNode.right;
        }
        int[] res=new int[list.size()];
        for(int i=0;i<list.size();i++){
            res[i]=list.get(i);
        }
        return res;
    }

}