题目:
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
import java.util.ArrayList;
import java.util.List;
public class _144_PreorderTraversal {
List<Integer> ans;
public List<Integer> preorderTraversal(TreeNode root) {
ans = new ArrayList<>();
traversal(root);
return ans;
}
public void traversal(TreeNode root) {
if(root == null) {
return;
}
ans.add(root.val);
traversal(root.left);
traversal(root.right);
}
}