leetcode: 98. Validate Binary Search Tree
来源:互联网 发布:脂老虎饼干 知乎 编辑:程序博客网 时间:2024/06/18 09:48
Q
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Example 1:
2 / \ 1 3
Binary tree [2,1,3], return true.
Example 2:
1 / \ 2 3
Binary tree [1,2,3], return false.
AC
# Definition for a binary tree node.class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution(object): def isValidBST(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True a, b, c= self.recurse(root) return c def recurse(self, root): if not root.left and not root.right: return (root.val, root.val, True) min_ = root.val max_= root.val if root.left: if root.val<=root.left.val: return (None, None, False) else: _min, _max, ok = self.recurse(root.left) if not ok: return (None, None, False) if _max>=root.val: return (None, None, False) min_= min(min_, _min) if root.right: if root.val>=root.right.val: return (None, None, False) else: _min, _max, ok = self.recurse(root.right) if not ok: return (None, None, False) if _min<=root.val: return (None, None, False) max_= max(max_, _max) return (min_, max_, True)# Time: O(n)# Space: O(1)# Definition for a binary tree nodeclass TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None# Morris Traversal Solutionclass Solution2(object): def isValidBST(self, root): prev, cur = None, root while cur: if cur.left is None: if prev and prev.val >= cur.val: return False prev = cur cur = cur.right else: node = cur.left while node.right and node.right != cur: node = node.right if node.right is None: node.right = cur cur = cur.left else: if prev and prev.val >= cur.val: return False node.right = None prev = cur cur = cur.right return True# Time: O(n)# Space: O(h)class Solution3(object): def isValidBST(self, root): return self.isValidBSTRecu(root, float("-inf"), float("inf")) def isValidBSTRecu(self, root, low, high): if root is None: return True return low < root.val and root.val < high \ and self.isValidBSTRecu(root.left, low, root.val) \ and self.isValidBSTRecu(root.right, root.val, high)if __name__ == "__main__": root, root.left, root.right = TreeNode(2), TreeNode(1), TreeNode(3) assert Solution().isValidBST(root) == True
阅读全文
1 0
- LeetCode 98. Validate Binary Search Tree
- [LeetCode]98.Validate Binary Search Tree
- [Leetcode] 98. Validate Binary Search Tree
- LeetCode --- 98. Validate Binary Search Tree
- [leetcode] 98.Validate Binary Search Tree
- [leetcode] 98.Validate Binary Search Tree
- [Leetcode] 98. Validate Binary Search Tree @python
- Leetcode 98. Validate Binary Search Tree
- [LeetCode]98. Validate Binary Search Tree
- leetcode 98. Validate Binary Search Tree
- *LeetCode 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree LeetCode
- LeetCode 98. Validate Binary Search Tree
- LeetCode 98. Validate Binary Search Tree
- LeetCode *** 98. Validate Binary Search Tree
- 98. Validate Binary Search Tree-leetcode-java
- [leetcode] 98. Validate Binary Search Tree
- LeetCode - 98. Validate Binary Search Tree
- springcloud(六):配置中心git示例
- 基于Qt的SocketIO客户端实现 【三】发送与接收
- springcloud(七):配置中心svn示例和refresh
- 常用Benchmark
- ls1021aqds uboot nand boot
- leetcode: 98. Validate Binary Search Tree
- springcloud(八):配置中心服务化和高可用
- 腾讯2017秋招笔试编程题:素数对 [python]
- 写给小白看的 JavaScript 异步
- 2017-11-22 6周3次课 正则------awk
- SQL优化
- springcloud(九):配置中心和消息总线(配置中心终结版)
- win10+Anaconda5.0.1 python3.6 编译caffe的坑
- Merge Sorted Array:归并有序数组