トピック:
トピックリンク: https://leetcode-cn.com/problems/validate-binary-search-tree/
問題解決のアイデア:
左下のサブツリーからアップし、各ノードは、番号は、バイナリサーチの要件を満たしていない場合、それは偽を返し、すべての再帰の終了をサイズの独自の値を決定し、彼らが要件を満たしている場合、現在の値は、リスト1のレベルに戻ります再帰的に
コードの実装:
# 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, [])