前序遍历(根左右)
function fn(root){
if (root == null) return [] ;
const result = [];
function preorder(root) {
if (root == null) return;
result.push(root.val);
preorder(root.left, result);
preorder(root.right, result);
}
preorder(root);
return result;
}
function fn(root){
const res = []
const stack = []
if(root) stack.push(root)
while (stack.length) {
const n = stack.pop()
res.push(n.val)
if(n.right) stack.push(n.right)
if(n.left) stack.push(n.left)
}
return res
}
中序遍历(左根右)
function fn(root){
const res = [];
function inorder(root) {
if (root == null) return;
inorder(root.left);
res.push(root.val);