zoukankan      html  css  js  c++  java
  • 98. 验证二叉搜索树



    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
    

  • 相关阅读:
    Angular2使用boostrap和ng-bootstrap总结
    Java
    Java
    Java 13
    Java 12
    Java 11
    Java 9
    Java 8- Java 分支结构
    Java 7-Java 循环结构
    Java 6- Java 运算符
  • 原文地址:https://www.cnblogs.com/panweiwei/p/13585564.html
Copyright © 2011-2022 走看看