leetcode:94. 二叉树的中序遍历(java)

 

class Solution {
     public static List res;

    public List inorderTraversal(TreeNode root) {
        res = new ArrayList<>();
        dfs(root);
        return res;
    }

    public void dfs(TreeNode root) {
        if (root == null) return;
        dfs(root.left);
        res.add(root.val);
        dfs(root.right);
    }
}

 

你可能感兴趣的:(算法试题(java))