TLE in scala buttom-up method, any suggestion?


  • 0
    Z

    Hi, I used following scala script.It is a typical button-top algorithm.
    but there was a TLE error when I did the submission.
    Does anyone have the same problem as mine?
    BTW, Why does scala code which might be just identical with Java word by word will always take 10 or 100 times more running time than java consumed on leetcode?
    Anyone have any idea?

       object Solution {
        def isBalanced(root: TreeNode): Boolean = {
          def check(node:TreeNode):Int = {
            if (node==null) return 0
            val right = check(node.right)
            val left = check(node.left)
            if (check(node.right)== -1 || check(node.left)== -1 || math.abs(left-right)<=1)
              return -1
            else
              math.max(left,right) 
          }
    
          if (root == null) return true
          val l = check(root.left)
          val r = check(root.right)
          l!= -1 && r != -1 && math.abs(l-r)<= 1
        }
    }
    

Log in to reply
 

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