leetcode--Validate Binary Search Tree
来源:互联网 发布:万达电商 淘宝卖货 编辑:程序博客网 时间:2024/06/10 02:52
问题描述:
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.
采用中序遍历,如果遍历过程严格服从从小到大,则在最后返回True,否则返回False
# Definition for a binary tree node# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param root, a tree node # @return a boolean def isValidBST(self, root): def inOrder(root): stack = [] p = root minVal = None while p or len(stack) > 0: if p: stack.append(p) p = p.left else: p = stack.pop() if None == minVal or minVal < p.val: minVal = p.val p = p.right else: return False return True return inOrder(root)
0 0
- LeetCode: Validate Binary Search Tree
- [Leetcode] Validate Binary Search Tree
- LeetCode : Validate Binary Search Tree
- [LeetCode] Validate Binary Search Tree
- [LeetCode] Validate binary search tree
- [LeetCode]Validate Binary Search Tree
- [Leetcode]Validate Binary Search Tree
- [leetcode]Validate Binary Search Tree
- LeetCode-Validate Binary Search Tree
- [leetcode] Validate Binary Search Tree
- LeetCode - Validate Binary Search Tree
- [LeetCode] Validate Binary Search Tree
- Leetcode Validate Binary Search Tree
- LeetCode | Validate Binary Search Tree
- LeetCode - Validate Binary Search Tree
- Leetcode: Validate Binary Search Tree
- 【Leetcode】Validate Binary Search Tree
- 【leetcode】Validate Binary Search Tree
- Consecutive Numbers
- new/delete和malloc/free异同
- HTML+CSS基础知识整理总结2015.3.10
- 【浅墨Unity3D Shader编程】之一 夏威夷篇:游戏场景的创建 & 第一个Shader的书写
- C++字符串操作整理
- leetcode--Validate Binary Search Tree
- 1008. Elevator
- 【2013-5】 day2 一系列生成树问题
- mongodb 详解 error:10061 由于目标计算机积极拒绝,无法连接
- ORACLE 内置函数之 GREATEST 和 LEAST
- JAVA中util类里的map/set/list/vector之间的关系
- hdu 5044 树链剖分+前缀和
- C++ 大数模板
- 2015年3月10日学习心得