如果是二叉查找树,那么其中序遍历是上升序列。
"""
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.mid_list = []
self.mid_order(root, self.mid_list)
for i in range(1, len(self.mid_list)):
if self.mid_list[i] <= self.mid_list[i-1]:
return False
return True
def mid_order(self, root, mid_list):
if root == None:
return
self.mid_order(root.left, self.mid_list)
self.mid_list.append(root.val)
self.mid_order(root.right, self.mid_list)