【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) ...
随机推荐
- TensorFlow CNN 測试CIFAR-10数据集
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50738311 1 CIFAR-10 数 ...
- Swift_4_闭包(Blocks)
import Foundation println("Hello, World!") var arr = [1,2,4,6,74,2] func hasClosure(list:[ ...
- ios json结构
NSString *itemJson = [NSString stringWithFormat:@"{\"Id\":\"%@\",\"Cha ...
- 解决Command "laravoole" is not defined.
版权声明:本文为博主原创文章,未经博主允许不得转载. GitHub地址:https://github.com/garveen/laravoole 先来执行正常的安装流程: 安装 要开始,将larav ...
- 将非递减有序排列(L L1)归并为一个新的线性表L2 线性表L2中的元素仍按值非递减
#include "stdio.h"#include "stdlib.h"#include "function.h"void main(){ ...
- 大华NVR设备接分别入宇视摄像机Onvif和RTSP主子码流的方案说明
需求提要 1.各个内网现场有多种网络摄像机IPC和网络硬盘录像机NVR设备: 2.需要将这些设备统一接入到云端中心平台,进行统一的视频直播和录像回放管理: 3.由于目前IPC设备都属于高清设备,主码流 ...
- EF之POCO应用系列2——复杂类型
在.NET开发中,EF4以前的版本以及LINQ TO SQL都不支持complex数据类型,EF4终于支持complex类型的数据了,这意味着微软的EF框架朝领域驱动方面又迈了一大步. 复杂的数据类型 ...
- 将本地jar包手动复制到Maven库中,在其它电脑上用Maven打包时出错
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/UP19910522/article/details/31396107 背景交代:在做图片水印时候引入 ...
- ubuntu防火墙
开启防火墙 sudo ufw enable 关闭防火墙 sudo ufw disable 查看防火墙状态 sudo utw status
- 转 开启“大数据”时代--大数据挑战与NoSQL数据库技术 iteye
一直觉得“大数据”这个名词离我很近,却又很遥远.最近不管是微博上,还是各种技术博客.论坛,碎碎念大数据概念的不胜枚举. 在我的理解里,从概念理解上来讲,大数据的目的在于更好的数据分析,否则如此大数据的 ...