lib/common/tree_utils.rb
#
# original iterative version  using an iterative in-order traversal
# we use a stack to keep track of the node, so treat rnode
# before lnode
# 
original iterative version  using an iterative in-order traversal
we use a stack to keep track of the node, so treat rnode
before lnode
original iterative version using an iterative in - order traversal we use a stack to keep track of the node so treat rnode before lnode
def depth_iter(root=@root)
      return 0 if root.nil?  
      s = []
      h = 1
      s.push [root, h]
      loop do
        break if s.empty?
        node, _h = s.pop
        h = _h if h < _h 
        s.push [node.rnode, _h+1] if node.rnode
        s.push [node.lnode, _h+1] if node.lnode
      end
      h
    end
def depth_iter ( root = @root ) return 0 if root . nil? s = [ ] h = 1 s . push [ root , h ] loop do break if s . empty? node , _h = s . pop h = _h if h < _h s . push [ node . rnode , _h + 1 ] if node . rnode s . push [ node . lnode , _h + 1 ] if node . lnode end h end
original iterative version  using an iterative in-order traversal
we use a stack to keep track of the node, so treat rnode
before lnode
original iterative version using an iterative in - order traversal we use a stack to keep track of the node so treat rnode before lnode
# height of an empty tree is 0 # nothing to do with node.val # interested by the max
root
<not_specific>