/***
 * Given a binary tree, return the preorder traversal of its nodes' values.
 *
 * For example:
 * Given binary tree {1,#,2,3},
 *    1
 *     \
 *      2
 *     /
 *    3
 * return [1,2,3].
 */
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.List;
import java.util.ArrayList;
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<Integer>();
        dfs(ret, root);
        return ret;
    }

    private void dfs( List<Integer> ret, TreeNode node) {
        if (node == null)
            return;
        ret.add(node.val);
        dfs(ret, node.left);
        dfs(ret, node.right);
    }
}

add BinaryTreePreorderTraversal.java