题解 | #判断是不是二叉搜索树#
判断是不是二叉搜索树
https://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @return bool布尔型 # class Solution: def __init__(self) -> None: self.arr = [] def LDR(self , this: TreeNode): if this: self.LDR(this.left) self.arr.append(this.val) self.LDR(this.right) def isValidBST(self , root: TreeNode) -> bool: # write code here self.LDR(root) arrsort = self.arr.copy() arrsort.sort() if self.arr == arrsort: return True else: return False