leetcode98 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.
给出一棵二叉树,判断是否符合给定的规则
思路:
考虑二叉树的中序遍历。
二叉树的中序遍历是升序的,因此中序遍历后判断序列是否是升序即可。
代码:
public static boolean isValidBST(TreeNode root){
if(root == null){
return true;
}
Stack<TreeNode> ts = new Stack<TreeNode>();
ArrayList<Integer> arr = new ArrayList<Integer>();
TreeNode ptr = root;
//中序遍历
while(ptr != null || !ts.isEmpty()){
while(ptr != null){
ts.push(ptr);
ptr = ptr.left;
} if( !ts.isEmpty() ){
ptr = ts.pop();
arr.add(ptr.val);
ptr = ptr.right;
}
}
int index = 0;
int[] arrs = new int[arr.size()];
for(int i: arr){
arrs[index++] = i;
}
//判断中序遍历得到的序列是否是升序的
index = arrs[0];
boolean flag = true;
for(int i = 1; i < arr.size(); i++){
if(index >= arrs[i]){
flag = false;
break;
}
index = arrs[i];
}
return flag;
}
代码优化:
上述方法中,首先是将中序序列存储在集合中,为了判断升序方便又转换为数组,比较繁琐。
其实无需存储中序序列,在中序遍历过程中进行判断即可,一旦不符合升序条件就返回false。
代码:
//代码优化(中序遍历过程中判断即可)
public static boolean isValidBST(TreeNode root){
if(root == null){
return true;
}
Stack<TreeNode> ts = new Stack<TreeNode>();
TreeNode ptr = root;
TreeNode pre = null;
//中序遍历
while(ptr != null || !ts.isEmpty()){
while(ptr != null){
ts.push(ptr);
ptr = ptr.left;
} if( !ts.isEmpty() ){
ptr = ts.pop();
if( pre != null && pre.val >= ptr.val){
return false;
}
pre = ptr;
ptr = ptr.right;
}
}
return true; }
leetcode98 Validate Binary Search Tree的更多相关文章
- (BST 递归) leetcode98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
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) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- ecshop 工作流程加载配置介绍
ecshop 工作流程加载配置介绍 分类: ecshop2014-09-14 09:36 729人阅读 评论(2) 收藏 举报 模板引擎工作流 这里简单介绍下echsop工作流程: 首先,你会发现一般 ...
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- SQL--查询相同字段的数据
select city, statefrom state_county_citywhere city in (select city from state_county_city group by c ...
- wordpress插入腾讯视频的方法
wordpress插入腾讯视频的方法 最近网站需要插入腾讯视频,但是腾讯视频目前没有分享代码,只有分享到微信,qq,微博等具体选项.百度这个问题,貌似没有很好地解决办法,好像有两个插件可以使用,安装试 ...
- PHP关闭提示、打印配置
打印配置 PHP.exe -i > Info.txt 关闭 PHP 提示的方法 搜索php.ini: error_reporting = E_ALL 改为: error_reporting = ...
- php javascript C 变量环境 块级作用域
<?php $w = 'w'; $wb = '123'.$w; $w = 'ww'; echo $wb; if(TRUE){ $wd = '123wd'; } echo $wd; if(FALS ...
- Delphi的哈希表(一)
哈希表是通过哈希值来访问的,通过一定的内存浪费获取检索速度,在检索项时是不需要逐一检索.在编程中有一定的好处. unit Unit1; interface uses Windows, Messages ...
- 微信公众账号开发教程(一) 基本原理及微信公众账号注册 ——转自http://www.cnblogs.com/yank/p/3364827.html
微信公众账号开发教程 基本原理 在开始做之前,大家可能对这个很感兴趣,但是又比较茫然.是不是很复杂?很难学啊? 其实恰恰相反,很简单.为了打消大家的顾虑,先简单介绍了微信公众平台的基本原理. 微信服务 ...
- 各大网站CSS代码初始化集合
css代码之所以初始化,是因为能尽量减少 各浏览器之间的兼容性问题! 腾讯QQ官网 样式初始化 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fiel ...
- 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建
Xamarin.Android 入门之:Xamarin+vs2015 环境搭建 一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...