recursion
1 public class Solution { 2 public ArrayList<Integer> preorderTraversal(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 ArrayList<Integer> result = new ArrayList<Integer>(); 6 traversal(result, root); 7 return result; 8 } 9 private void traversal(ArrayList<Integer> result, TreeNode root) 10 { 11 if(root == null) 12 return; 13 result.add(root.val); 14 traversal(result, root.left); 15 traversal(result, root.right); 16 } 17 }