- 浏览: 183725 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 500Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 430Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 676Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 845Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 707For a undirected graph with tre ...
相关推荐
python python_leetcode题解之098_Validate_Binary_Search_Tree
js js_leetcode题解之98-validate-binary-search-tree.js
c语言基础 c语言_leetcode题解之0098_validate_binary_search_tree.zip
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...
1. **验证二叉搜索树 (Validate Binary Search Tree)**:给定一个二叉树,判断它是否是有效的二叉搜索树。 2. **二叉搜索树的最近公共祖先 (Lowest Common Ancestor of a Binary Search Tree)**:在二叉搜索树中找到...
Validate Binary Search Tree)和判断对称二叉树(101. Symmetric Tree)。这两个问题都涉及到对二叉树结构的深入理解和遍历策略。 首先,我们来看验证二叉搜索树。二叉搜索树(Binary Search Tree, BST)是一种...
- **Validate Binary Search Tree**:验证一个二叉树是否为二叉搜索树。 - **Recover Binary Search Tree**:恢复二叉搜索树中的两个错误节点。 - **Binary Tree Path**:找到二叉树中和为目标值的路径。 - **...
25. Validate Binary Search Tree:验证二叉搜索树的合法性。 26. Maximum Depth of Binary Tree:二叉树的最大深度。 27. Minimum Depth of Binary Tree:二叉树的最小深度。 28. Balanced Binary Tree:判断二叉树...
本压缩包文件“python-leetcode面试题解之第98题验证二叉搜索树-题解.zip”专注于解决LeetCode的第98题,即“验证二叉搜索树”(Validate Binary Search Tree)。下面我们将深入探讨这个话题,了解如何使用Python来...
25. Validate Binary Search Tree:验证二叉搜索树。 26. Maximum Depth of Binary Tree:计算二叉树的最大深度。 27. Minimum Depth of Binary Tree:计算二叉树的最小深度。 28. Balanced Binary Tree:判断一个...
- Validate Binary Search Tree(验证二叉搜索树) - Maximum Depth of Binary Tree(二叉树的最大深度) - Convert Sorted Array to Binary Search Tree(将有序数组转换为二叉搜索树) 7. Bit Manipulation...
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...
5. **验证二叉搜索树的有效性** (Validate Binary Search Tree) - 验证一个二叉树是否是二叉搜索树,可以通过中序遍历后检查序列是否严格升序来实现。 - 示例代码中,使用迭代中序遍历,在遍历过程中维护一个栈,...
在本压缩包“php-leetcode题解之验证二叉搜索树.zip”中,主要包含的是使用PHP语言解决LeetCode上的一道题目——验证二叉搜索树(Validate Binary Search Tree)。LeetCode是一个流行的在线编程挑战平台,它提供了...
- 例如,验证二叉搜索树(Validate Binary Search Tree)需要判断给定的二叉树是否符合二叉搜索树的定义。 - 计算二叉树的最大深度(Maximum Depth of Binary Tree)考察递归的使用。 - 平衡二叉树(Balanced Binary...
* [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]...
- Validate Binary Search Tree:验证给定的二叉树是否为有效的二叉搜索树(BST),这需要对二叉树的遍历和树节点值的区间限制有清晰的理解。 - Valid Parentheses:这是一个用来检测字符串中括号是否有效匹配的问题...
leetcode ...Validate Binary Search Tree - 二分查找 二分查找 + 数据缓存:1095. Find in Mountain Array 链表 有序链表合并:21. Merge Two Sorted Lists 回文 双指针判断回文:680. 验证回文字符串 Ⅱ
5. **二叉树**:二叉树问题包括遍历、查找、平衡调整等,如"二叉树的最大路径和"(Maximum Path Sum)、"验证二叉搜索树"(Validate Binary Search Tree)。 6. **回溯**:回溯是一种尝试所有可能解的算法策略,...