Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bfs<T>(tree: BSTNode<T>, fn: (n: T) => void) {
- const queue = [tree];
- while (queue.length) {
- const cur = queue.shift();
- if (cur.left) queue.push(cur.left);
- if (cur.right) queue.push(cur.right);
- fn(cur.value);
- }
- }
- a = [4, 5, 10, 2, 3, 1];
- b = toBST(a);
- list = [];
- bfs(b, n => list.push(n)); /** [4, 2, 5, 1, 3, 10] */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement