Recursive solution(Preorder)


#1

public int[] preorderTraversal(TreeNode A) {
ArrayList list=new ArrayList();
preorder(A,list);
int[] arr=new int[list.size()];
for(int i=0;i<list.size();i++){
arr[i]=list.get(i);
}
return arr;
}
private void preorder(TreeNode A, ArrayList list){
if(A==null){
return;
}
list.add(A.val);
preorder(A.left,list);
preorder(A.right,list);

}