题目:

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. osal_start_timerEx(Lock_TaskID,SBP_START_DEVICE_EVT,SBP_PERIODIC_EVT_PERIOD)的理解

    osal_start_timerEx(Lock_TaskID,SBP_START_DEVICE_EVT,SBP_PERIODIC_EVT_PERIOD)与osal_set_event(Music_Ta ...

  2. 20145235李涛 《Java程序设计》第3周学习总结

    类与对象 定义类 类是对象的“设计图”,对象是类的实际类型.另外,定义时用class,建实例用new. 通过书上的代码才有所理解: class Clothes { String color; char ...

  3. von Neumann architecture

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION 3.1 COMPUTER COMPONEN ...

  4. kudu

    Kudu White Paper http://www.cloudera.com/documentation/betas/kudu/0-5-0/topics/kudu_resources.html h ...

  5. Delphi 如何操作外部程序的控件(如按钮,文本框,单选按钮等)

    看你要做什么,比较现在网络很流行的QQ.MSN这些软件都屏蔽了,你可能还可以访问一些小软件的这些控制,思路及方案如下(API函数自己去百度查一下)1.得到你要这个窗口的句柄 使用FindWindow2 ...

  6. nodejs 基本类型和语法

    写在前面 今天想要查下Node的类型什么的知识,想要总结下,在Googol上看到一个文章,但是原始的链接不在了,在快照中把这篇文章拉出来,如果原作者有问题,请联系我! 该文章都是一些JS的基础,高手自 ...

  7. random circle

    <!doctype html><meta charset="utf-8"><html><head><title>D3 t ...

  8. oracle 表查询二

    1.使用逻辑操作符号问题:查询工资高于500或者是岗位为manager的雇员,同时还要满足他们的姓名首字母为大写的J?select * from emp where (sal > 500 or ...

  9. Emmet(之前叫Zencoding)插件安装

    按 ctr+shift+P,输入 install Package,找到emmet,确认安装 Package Control Messages======================== Emmet ...

  10. web调试工具-firebug

    Firebug是网页浏览器firefox下面的一款开发类插件.它集HTML查看和编辑,js控制台,网络状况监视器于一体,是开发js,css,HTML:和Ajax的得力助手 (自己整理的,有错误的话见谅 ...