A golang solution


  • 0
    N
    func isValidBST(root *TreeNode) bool {
    	if root != nil {
    		for i, v := range[]*TreeNode{root.Left, root.Right} {
    			var q []*TreeNode
    			if v != nil {
    				q = append(q, v)
    				for len(q) > 0 {
    					v := q[0]
    					q = q[1:]
    					if i == 0 && v.Val >= root.Val ||
    					   i == 1 && v.Val <= root.Val {
    						return false
    					}
    					for _, t := range []*TreeNode{v.Left, v.Right} {
    						if t != nil {
    							q = append(q, t)
    						}
    					}
    				}
    			}
    		}
    
    		return isValidBST(root.Left) && isValidBST(root.Right)
    	}
    	return true
    }

Log in to reply
 

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