Python | Leetcode Python题解之第530题二叉搜索树的最小绝对差
题目:
题解:
# Definition for a binary tree node.
# 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
"""
pre = -float('inf')
p = root
st = []
while p is not None or st:
while p is not None:
st.append(p)
p = p.left
p = st.pop()
if p.val > pre:
pre = p.val
else:
return False
p = p.right
return True