class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
inorderList = self.inorder(root)
i, j = 0, 1
while j < len(inorderList):
if inorderList[i] >= inorderList[j]:
return False
i += 1
j += 1
return True
# 中序遍历,迭代法
def inorder(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
mid_stack = []
ans = []
cur = root
while cur or mid_stack:
while cur:
mid_stack.append(cur)
cur = cur.left
temp = mid_stack.pop()
ans.append(temp.val)
cur = temp.right
return ans