Ruby Inorder Traversal


  • 0
    T
    def get_minimum_difference(root)
        min = 1.0/0.0
        values = []
        
        inorder_get_values(root, values)
        
        (1...values.length).each do |i|
            cur_min = values[i] - values[i - 1]
            
            min = [cur_min, min].min
        end
        p values
        min
    end
    
    def inorder_get_values(root, values)
        return if root.nil?
        
        inorder_get_values(root.left, values)
        values << root.val
        inorder_get_values(root.right, values)
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.