validate-binary-search-tree
This commit is contained in:
		
							
								
								
									
										28
									
								
								validate-binary-search-tree/sol.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								validate-binary-search-tree/sol.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
type TreeNode struct {
 | 
			
		||||
	Val   int
 | 
			
		||||
	Left  *TreeNode
 | 
			
		||||
	Right *TreeNode
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Definition for a binary tree node.
 | 
			
		||||
 * type TreeNode struct {
 | 
			
		||||
 *     Val int
 | 
			
		||||
 *     Left *TreeNode
 | 
			
		||||
 *     Right *TreeNode
 | 
			
		||||
 * }
 | 
			
		||||
 */
 | 
			
		||||
func isValidSubBST(root *TreeNode, min, max int) bool {
 | 
			
		||||
	if root == nil {
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	if root.Val < min || root.Val > max {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	return isValidSubBST(root.Left, min, root.Val-1) && isValidSubBST(root.Right, root.Val+1, max)
 | 
			
		||||
}
 | 
			
		||||
func isValidBST(root *TreeNode) bool {
 | 
			
		||||
	return isValidSubBST(root, -1<<31, 1<<31)
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user