Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. function bfs<T>(tree: BSTNode<T>, fn: (n: T) => void) {
  2. const queue = [tree];
  3.  
  4. while (queue.length) {
  5. const cur = queue.shift();
  6.  
  7. if (cur.left) queue.push(cur.left);
  8. if (cur.right) queue.push(cur.right);
  9.  
  10. fn(cur.value);
  11. }
  12. }
  13.  
  14. a = [4, 5, 10, 2, 3, 1];
  15. b = toBST(a);
  16.  
  17. list = [];
  18. bfs(b, n => list.push(n)); /** [4, 2, 5, 1, 3, 10] */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement