lib/common/tree_utils.rb
# Breadth first, using a queue (space consumming)
#
Breadth first, using a queue (space consumming)
Breadth first using a queue ( space consumming )
def _level_order(root, ary=[], lbd)
      ary, q = [], BST::MyQueue.new
      q.enqueue root
      loop do
        break if q.empty?
        node = q.dequeue
        ary << lbd.call(node.val)
        q.enqueue node.lnode if node.lnode          
        q.enqueue node.rnode if node.rnode
      end
      ary      
    end
def _level_order ( root , ary = [ ] , lbd ) ary , q = [ ] , BST :: MyQueue . new q . enqueue root loop do break if q . empty? node = q . dequeue ary << lbd . call ( node . val ) q . enqueue node . lnode if node . lnode q . enqueue node . rnode if node . rnode end ary end
Breadth first, using a queue (space consumming)
Breadth first using a queue ( space consumming )
root
<not_specific>
ary
<not_specific>
lbd
<not_specific>