function preOrder(root, array = []) {
if (root) {
//根左右
array.push(root.val);
preOrder(root.left, array);
preOrder(root.right, array);
}
return array;
}
const preOrder = function (root) {
const result = [];
const stack = [];
let current = root;
while (current || stack.length > 0) {
while (current) {
result.push(current.val);
stack.push(current);
current = current.left;
}
current = stack.pop();
current = current.right;
}
return result;
};