-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateBinarySearchTree.py
More file actions
29 lines (26 loc) · 937 Bytes
/
validateBinarySearchTree.py
File metadata and controls
29 lines (26 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/validate-binary-search-tree/
# Author: Miao Zhang
# Date: 2021-01-16
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
if not root:
return True
return self.isVal(root, -(2**32), 2**32)
def isVal(self, node, minv, maxv):
if not node:
return True
if node.left:
if node.left.val >= node.val or node.left.val <= minv:
return False
if node.right:
if node.right.val <= node.val or node.right.val >= maxv:
return False
return self.isVal(node.left, minv, node.val) and self.isVal(node.right, node.val, maxv)