SHARE
TWEET

Untitled

a guest Jul 17th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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] */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top