topic:
Topic links: https://leetcode-cn.com/problems/validate-binary-search-tree/
Problem-solving ideas:
Up from the bottom left sub-tree and each node determines its own value of the size, if the number does not meet the requirements of the binary search, it returns false, all recursion ends; if they meet the requirements, the current value is returned to the list one level, recursively
Code:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
def valid_help(root, path):
if not root:
return True
left = []
if root.left and False == valid_help(root.left, left):
return False
right = []
if root.right and False == valid_help(root.right, right):
return False
if 0 < len(left) and max(left) >= root.val:
return False
elif 0 < len(right) and min(right) <= root.val:
return False
path += left + right + [root.val]
return True
return valid_help(root, [])