
思路:
- 使用中序遍历,将所有数值存在一个数组里,如果是按increasing顺序排列,则是一个二叉搜索树。
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param: root: The root of binary tree.
@return: True if the binary tree is BST, or false
"""
def isValidBST(self, root):
# write your code here
self.list = []
if not root:
return True
def build_list(root):
if root:
build_list(root.left)
self.list.append(root.val)
build_list(root.right)
return
build_list(root)
for i in range(0, len(self.list)-1):
if self.list[i]< self.list[i+1]:
continue
else:
return False
return True
网友评论