Binary Tree Preorder Traversal 2021-07-08 01:46
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
if (root == null) {
return res;
}
res.add(root.val);
res.addAll(preorderTraversal(root.left));
res.addAll(preorderTraversal(root.right));
return res;
}
Runtime | Memory |
---|---|
0 ms | 36.8 MB |
EOF