题解 | #判断是不是二叉搜索树#
判断是不是二叉搜索树
https://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b?tpId=295&tqId=2288088&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
from re import T import sys # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @return bool布尔型 # class Solution: def isValidBST(self, root: TreeNode) -> bool: # write code here ''' 中序遍历收集序列的值,如果值递增为二叉排序树 ''' pre=-sys.maxsize-1 res=[] def middletravel(root,pre): #re=pre if not root: return True middletravel(root.left,pre) #if root.val <pre: # return False res.append(root.val) pre=root.val middletravel(root.right,pre) middletravel(root,pre) for i in range(len(res)-1): if res[i+1]<res[i]: return False return True