Simplest answer in Java?


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

Log in to reply
 

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