题目:

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. 状态模式 java && php

    状态模式 java && php     状态模式 输入信号是事件:输出是状态结果,状态模式状态机就是一个黑盒子.状态模式主要突出了两个字:”改变”,对象的状态决定了状态的行为,事物的 ...

  2. Yii源码阅读笔记(十四)

    Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: /** * Returns a list of scenarios and the corresponding active attr ...

  3. JLINK SWD下载模式引脚

    SWD接4个脚就可以  RESET可以不用接

  4. 20145235 《Java程序设计》第8周学习总结

    教材学习内容总结 15.1.1日志API简介 使用日志的起点是logger类,logger实例的创建有许多要处理的要素,必须使用logger的静态方法getLogger(). 通常在哪个类上取得的lo ...

  5. Amoeba基本配置

    Amoeba安装及读写分离配置一.amoeba简介官网:http://docs.hexnova.com/amoeba/index.html二.Centos下安装jdk1.yum 安装1.6版本jdk2 ...

  6. buffer overflow

    Computer Systems A Programmer's Perspective Second Edition We have seen that C does not perform any ...

  7. double-clicking

    <!doctype html> <button id="id0" onclick="w('id0','str0')">target0&l ...

  8. B-Tree indexs

    mysql_High.Performance.MySQL.3rd.Edition.Mar.2012 A B-Tree index speeds up data access because the s ...

  9. Delphi指针的用法

    DELPHI指针的使用 大家都认为,C语言之所以强大,以及其自由性,很大部分体现在其灵活的指针运用上.因此,说指针是C语言的灵魂,一点都不为过.同时,这种说法也让很多人产生误解,似乎只有C语言的指针才 ...

  10. HelloWorld之jetty运行

    jetty是一个轻便的嵌入式servlet容器.其启动运行非常简单.eclipse下运行jetty容器有如下几步, 一.建一个普通的java工程 二.把jetty需要的包导入工程分别是jetty-6. ...