Java Simple Recursion


  • 0
    R
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if(root == null) return res;
        res.addAll(inorderTraversal(root.left));
        res.add(root.val);
        res.addAll(inorderTraversal(root.right));
        return res;
    }

  • 1
    R

    Note: Recursive solution is trivial, could you do it iteratively?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.