Given the root of a binary tree, return the preorder traversal of its nodes' values.
https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg
Example 1:
Input: root = [1,null,2,3]
Output: [1,2,3]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [1]
Output: [1]
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
preOrder(root, res);
return res;
}
public void preOrder(TreeNode root, List<Integer> list){
if(root == null) return;
list.add(root.val);
preOrder(root.left, list);
preOrder(root.right, list);
}
}