Python 55 ms beats 99.0% using BFS


  • 0
    C
    class Solution(object):
        def isValidBST(self, root):       
            if not root:
                return True        
            q = [[root,-float('inf'),float('inf')]]
            while len(q) != 0:
                top,minRange,MaxRange = q.pop(0)
                if minRange >= top.val or top.val >= MaxRange :
                        return False
                if top.left:
                    q.append([top.left,minRange,top.val])
                if top.right:
                    q.append([top.right,top.val,MaxRange])
            return True
    

Log in to reply
 

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