【leetcode刷题笔记】Validate Binary Search Tree
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.
题解:BST valid 的充分必要条件是它的中序遍历是一个有序序列。
递归实现树的中序遍历,用私有变量lastVal记录上一个遍历的节点的值。在一次递归,首先递归判断左子树是否是BST,并且更新lastVal,然后将root的值跟lastVal比较,看root的值是否大于lastVal;然后递归判断右子树是否是BST。
代码如下:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int lastVal = Integer.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if(root == null)
return true; if(!isValidBST(root.left))
return false; if(root.val <= lastVal)
return false;
lastVal = root.val;
if(!isValidBST(root.right))
return false;
return true;
}
}
题目的关键点是lastVal更新的时机和与root比较的时机。
【leetcode刷题笔记】Validate Binary Search Tree的更多相关文章
- LeetCode之“树”:Validate Binary Search Tree
题目链接 题目要求: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...
- 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 ...
- LeetCode算法题-Trim a Binary Search Tree(Java实现)
这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【leetcode刷题笔记】Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- 移动端实用的meta标签
直接上代码,代码自有颜如玉 代码自有黄金屋啊 <meta http-equiv="Content-Type" content="text/html; charset ...
- Android SQLite基本用法
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...
- 安装Hadoop 1.1.2 (三 安装配置Hadoop)
1 tar -zxvf hadoop-1.1.2.tar.gz 2 在hadoop/conf目录 (1) 编辑 hadoop-env.sh export JAVA_HOME=/usr/java/jdk ...
- mysql数据库的导出与导入
导出 在dos节目,切换到mysql依照文件夹的bin下.输入下面命令 mysqldump -u root -p nxu_life > nxu_life2.sql 运行完毕后.就能够看到在bin ...
- COGS 1507. [IOI2000]邮局
1507. [IOI2000]邮局 ★☆ 输入文件:postoffice.in 输出文件:postoffice.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] ...
- Delphi 对话框实现源码分析
Delphi 对话框实现源码分析 简介 在这篇文章中,我将大概的从Delphi XE2 的Dialogs单元入手,分析ShowMessage,MessageBox等对话框运行原理,希望能帮助你理解 ...
- linux c编程:进程控制(四)进程调度
当系统中有多个进程到时候,哪个进程先执行,哪个进程后执行是由进程的优先级决定的.进程的优先级是由nice值决定的.nice值越小,优先级越高.可以看做越友好那么调度优先级越低.进程可以通过nice函数 ...
- centos下安装pip时失败:
[root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, securityLoading m ...
- Computer Vision: Algorithms and ApplicationsのImage processing
实在是太喜欢Richard Szeliski的这本书了.每一章节(after chapter3)都详述了该研究方向比較新的成果.还有很多很多的reference,假设你感兴趣.全然能够看那些參考论文 ...
- 搜索ABAP程序代码中的字符串
标准程序名:RPR_ABAP_SOURCE_SCAN /BEV1/NERM07DOCS