[LeetCode] 98. Validate Binary Search Tree_Medium
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:
Input:
2
/ \
1 3
Output: true
Example 2:
5
/ \
1 4
/ \
3 6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
is 5 but its right child's value is 4. 我们用LeetCode questions conlusion_InOrder, PreOrder, PostOrder traversal里面inorder的思路和code, 只需要判断是否ans是升序即可.
04/20/2019 Update : add one more solution using Divide and conquer. (add a class called returnType)
1. recursive S: O(n)
class Solution:
def validBST(self, root):
def helper(root):
if not root: return
helper(root.left)
ans.append(root.val)
helper(root.right)
ans = []
helper(root)
for i in range(1, len(ans)):
if ans[i] <= ans[i-1]: return False
return True
2. iterable S; O(n) . 因为有stack
class Solution:
def validBST(self, root):
stack, pre = [], None
while stack or root:
if root:
stack.append(root)
root = root.left
else:
node = stack.pop()
if pre != None and node.val <= pre:
return False
pre = node.val
root = node.right
return True
3. Divide and Conquer
class ResultType:
def __init__(self, isBST, minVal, maxVal):
self.isBST = isBST
self.minVal = minVal
self.maxVal = maxVal class Solution:
def validBST(self, root):
def helper(root):
if not root:
return ResultType(True, float('inf'), float('-inf'))
left = helper(root.left)
right = helper(root.right)
if not left.isBST or not right.isBST or (root.left and root.val <= left.maxVal) or (root.right and root.val >= right.minVal):
return ResultType(False, 0, 0)
return ResultType(True, min(root.val, left.minVal), max(root.val, right.maxVal))
return helper(root).isBST
4. S: O(1)
class Solution:
def validBST(self, root):
ans = [None, True] # pre node, answer def helper(root, ans):
if not root: return
helper(root.left, ans)
if ans[1] and ans[0] and ans[0].val >= root.val:
ans[1] = False
ans[0] = node
helper(root.right, ans) helper(root, ans)
return ans[1]
[LeetCode] 98. Validate Binary Search Tree_Medium的更多相关文章
- [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Leetcode 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- leetcode 98 Validate Binary Search Tree ----- java
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆
描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
随机推荐
- E - Coin Game
After hh has learned how to play Nim game, he begins to try another coin game which seems much easie ...
- js的with语句,和debugger语句
减少操作,速度及慢, 严格模式无法使用 debugger 在程序中打断点 'use strict' var name = 'global'; var obj = { name: "ajanu ...
- html5__Notifications API 桌面通知
MDN地址 google 文档 https://developers.google.cn/web/fundamentals/push-notifications/ const koa2 = requi ...
- ifnull与nvl
mysql 用 ifnull ,oracle没有ifnull 但是有相应的替换函数 nvl NVL(eExpression1, eExpression2)
- Maven知识点积累二
①maven常用命令: mvn clean:清除target下编译生成的class文件 mvn compile:编译 mvn package:打包放到target下 mvn install:打包并放到 ...
- 设置shell脚本静默方式输入密码方法
stty命令是一个终端处理工具.我们可以通过它来实现静默方式输入密码,脚本如下 #!/bin/sh echo –e “enter password:” stty –echo ...
- MySQL8.0安装连接Navicat的坑
刚在官网装好MySQL8.0后,我的cmd识别不了启动数据库的指令 需要cd到MySQL的bin目录配置mysql mysqld --install mysqld --remove mysql -u ...
- Node的REPL环境
1. Node的REPL环境 什么是REPL REPL全称 Read-eval-print-loop,交互式解析器 REPL可以提供给程序员对Node.js的一些api快速测试 REPL的基本操作 定 ...
- Xcode 9,真机测试,App installation failed
真机测试:能够build成功,但是 报错App installation failed A valid provisioning profile for this executable was not ...
- Xcode编辑器之基本使用(一)
前言. 苹果原生xcode使用介绍文档 1.Xcode IDE概览 说明: 从左到右,依次是“导航窗格(Navigator)->边列(Gutter)->焦点列(Ribbon)->代码 ...