- 浏览: 176251 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
题目要求判断一颗树是不是二叉搜索数,有关二叉搜索树的性质在这里就不在赘述了。我们采用递归的思想,借助两个变量max,和min,从根节点开始,递归比较每一颗子树,对于左子树的递归我们让max等于子树root的值,对于右子树我们让min等于子树root的值。代码如下:
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.
题目要求判断一颗树是不是二叉搜索数,有关二叉搜索树的性质在这里就不在赘述了。我们采用递归的思想,借助两个变量max,和min,从根节点开始,递归比较每一颗子树,对于左子树的递归我们让max等于子树root的值,对于右子树我们让min等于子树root的值。代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isValidBST(TreeNode root) { return isValid(root, null, null); } public boolean isValid(TreeNode root, Integer min, Integer max) { if(root == null) return true; if(min != null && root.val <= min) return false; if(max != null && root.val >= max) return false; return isValid(root.left, min, root.val) && isValid(root.right, root.val, max); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 235Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 238You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 352Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 345Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 469Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 533Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 441Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 631Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 440The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 402Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 525Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 547Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 384All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 875Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 895Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 570Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 617Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 776Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 748You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 640For a undirected graph with tre ...
相关推荐
Validate Binary Search Tree Convert Sorted Array to Binary Search Tree Convert Sorted List to Binary Search Tree LCA of BST Kth Smallest Element in a BST 二叉树的递归 Minimum Depth of Binary Tree ...
Validate Binary Search Tree - Java Recursive - Java Iterative - Java Inorder 0099 Recover Binary Search Tree - Java Recursive 0101 Symmetric tree - Java Recursive - Java Iterative - C Recursive...
作者: 负雪明烛个人博客: :
Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate Reverse Polish Notation #169 Majority Element #171...
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
leetcode ...Validate Binary Search Tree - 二分查找 二分查找 + 数据缓存:1095. Find in Mountain Array 链表 有序链表合并:21. Merge Two Sorted Lists 回文 双指针判断回文:680. 验证回文字符串 Ⅱ
jquery.validate.1.9.0.min.js jquery.validate.1.12.0.min.js jquery.validate.1.13.1.min.js jquery.validate.1.16.0.min.js jquery.validate.1.14.0.min.js jquery.validate.1.15.1.min.js jquery.validate....
validate formvalidate formvalidate form
jQueryValidate
这是一个关于jquery_validate插件学习的总结,内容不多,但是都是干货,有兴趣的可以看一下。
validate()可以指定校验范围内,或者是全局的 字段。而validateAll()只能校验全局。这篇文章主要介绍了vue+VeeValidate 校验范围(部分校验,全部校验) ,需要的朋友可以参考下
javascript表单验证:validate.js
jquery validate例子 能用
hibernate validate 中文文档 英文文档 hibernate validate 中文文档 英文文档 hibernate validate 中文文档 英文文档
jquery-validate 可对前端form进行表单校验,文档里详细介绍了如何使用jquery-validate框架和如何扩展验证方法。
jquery.validate.jsjquery.validate.jsjquery.validate.jsjquery.validate.jsjquery.validate.jsjquery.validate.jsjquery.validate.jsjquery.validate.jsjquery.validate.jsjquery.validate.jsjquery.validate.js...
jquery.validate.js jquery.validate.js
自做的一个简单的表单验证 jquery validate 表单验证 jquery validate 表单验证
jquery validate 使用详解
jquery validate 验证手册