4 lines in Ruby, beats 100%


  • 0
    A
    def assert(tree)
      return 0 if not tree
      left, right = assert(tree.left), assert(tree.right)
      (left - right).abs > 1 ? raise : 1 + [left, right].max
    end
    
    def is_balanced(root)
      assert(root) && true rescue false
    end

Log in to reply
 

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