9.12日yelp电话面试



  •    foo
      /   \
    baz   bar
     |
    quux
    
    order: foo -> baz -> bar -> quux
    
    
     class Node {
         public List<Node> children;
         public String value;
     }
    
    class BFS {
    
      public BFS(Node root) {
          todo = new QueueList<TreeNode>();
          this.root = root;
      }
      
      //returns the current value and increments iterator
      public Node next() {
          
          Tree<E> current = todo.dequeue();
          E result = current.value();
          if(!current.left().isEmpty())
              todo.enqueue(current.left());
          if(!current.right().isEmpty())
              todo.enqueue(current.right());
          return result;
          
      }
      
      //return if more nodes are being considered
      public boolean hasNext() {
          return !todo.isEmpty();
      }
    
    }
    
    BFS bfs = new BFS(foo);
    
    bfs.next(); // foo
    bfs.next(); // baz
    ...
    bfs.next(); // throw NoSuchElementException
    
    

登录后回复
 

与 BitTiger Community 的连接断开,我们正在尝试重连,请耐心等待