Skip to content

Instantly share code, notes, and snippets.

@yschen25
Created April 7, 2022 16:10
const preorder = (node, acc = []) => {
if (!node) return acc;
const { value, left, right } = node;
acc.push(value);
preorder(left, acc);
preorder(right, acc);
return acc;
};
console.log(preorder(tree)); // [0, 1, 3, 6, 7, 4, 2, 5, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment