自学内容网 自学内容网

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

原文地址:https://blog.csdn.net/Mopes__/article/details/143460224

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!