题目:

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的更多相关文章

  1. (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 ...

  2. Leetcode98. Validate Binary Search Tree验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  6. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  7. 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 ...

  8. [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 ...

  9. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. nginx下增加模块

    1.使用nginx -V确定nginx的编译参数2.下载nginx源码3.下载nginx的扩展模块(此处为memcache模块)4.进入nginx源码目录5../configure  --prefix ...

  2. PHP关闭提示、打印配置

    打印配置 PHP.exe -i > Info.txt 关闭 PHP 提示的方法 搜索php.ini: error_reporting = E_ALL 改为: error_reporting = ...

  3. Defining Stored Programs

    ok DROP PROCEDURE IF EXISTS truncate_insert_rank_month; DELIMITER /w/ CREATE PROCEDURE truncate_inse ...

  4. ubuntu 工作区中拖动一个窗体到另一个工作区就卡住回不到桌面了

    ubuntu 工作区中拖动一个窗体到另一个工作区就卡住回不到桌面了 解决方法: 按 alt + 回车  键直接就返回去了

  5. 【转】Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...

  6. iOS开发之Objective-c的MD5/SHA1加密算法的实现

    Objective-c实现MD5和SHA1算法相对还是比较简单的,可以直接调用系统的C/C++共享库来实现调用 MD5即Message Digest Algorithm 5(信息-摘要算法 5),用于 ...

  7. 安装mssql2008和启动时出现的问题及解决办法

    (一) 安装sql server 2008 时, 提示错误:此计算机上安装了 Microsoft Visual Studio 2008 的早期版本. 请在安装 SQL Server 2008 前将 V ...

  8. 50道JavaScript经典题和解法(JS新手进...持续更新...)

    最近在学习<数据结构与算法JavaScript描述>这本书,对JavaScript的特性和数据结构都有了进一步的了解和体会. 学习之余,也进行了相应的练习,题目难度不大,但是对所学知识的巩 ...

  9. [LeetCode]题解(python):062 Unique path

    题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x  ...

  10. Spark Programming--WordCount

    首先在$SPARK_HOME主目录下建立一个test文件夹,里面放一些文件(注意文件全部内容都可被hadoop用户访问,否则运行会出现permission denied的错误) 打开pyspark: ...