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.

解题思路:

本题方法多多,可以采用DFS,当然最简答的方法是采用之前的中序遍历Java for LeetCode 094 Binary Tree Inorder Traversal检查得到的List是否有序即可。

    public boolean isValidBST(TreeNode root) {

        List<Integer> list=inorderTraversal(root);
if(list.size()==0)
return true;
int temp=list.get(0);
for(int i=1;i<list.size();i++){
if(temp>=list.get(i))
return false;
temp=list.get(i);
}
return true;
}
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
if (root.left != null)
list.addAll(inorderTraversal(root.left));
list.add(root.val);
if (root.right != null)
list.addAll(inorderTraversal(root.right));
return list;
}

JAVA实现如下:

Java for LeetCode 098 Validate Binary Search Tree的更多相关文章

  1. 【leetcode】Validate Binary Search Tree

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

  2. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  3. [LeetCode]题解(python):098 Validate Binary Search Tree

    题目来源 https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it ...

  4. leetcode 98 Validate Binary Search Tree ----- java

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. [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 as ...

  6. Java for LeetCode 099 Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

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

  8. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 【题解】【BST】【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 ...

随机推荐

  1. MFC中 给基于CFormView的单文档添加背景图片

    关于基于CFormView的单文档应用程序,添加一个图片背景的方法之一如下: 下面是利用LoadImage实现.(先在程序目录中添加背景图片back.bmp) 1.在view类中添加类成员变量:(为C ...

  2. 《C++编程思想》第二章 数 据 抽 象(原书代码+习题+答案)

    相关代码例如以下: 1. <span style="font-size:18px;">/*声明与定义的差别*/ #include <iostream> us ...

  3. window脚本命令学习(转)

    批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用Cmd.exe按照该文件中各个命令出现 ...

  4. AutoCAD如何将dwf转成dwg格式

    dwf转成dwg怎么转, 悬赏分:30 - 解决时间:2009-11-22 10:19 重金:dwf转成dwg怎么转, 我是用在出图上的. 最佳答案 Design Web Format (DWF) 文 ...

  5. 分析cocos2d-x的lua项目中的工具方法

    在创建完cocos2d-x的lua项目后.打开项目的Resources中的extern.lua文件.里面有两个用于面向对象的方法.一个是用于克隆,一个是用于继承. 代码分析例如以下 --克隆一个对象 ...

  6. struts2获取前台提交的参数

      CreateTime--2017年8月25日16:30:11 Author:Marydon struts2对获取前台提交参数的封装 需要导入: import java.util.Enumerati ...

  7. 成都传智播客Java/PHP培训就业率高

    依据传智播客的数据统计,传智播客的学员有五分之中的一个的能在毕业前找到惬意的工作,一半的学员能在毕业后一个月之内找到惬意的工作,一般在毕业后两个月之内绝大多数同学都能找到惬意的工作.而且传智播客毕业学 ...

  8. java移位运算符详解

    http://soft.chinabyte.com/database/195/11553695.shtml java移位运算符不外乎就这三种:<<(左移).>>(带符号右移)和 ...

  9. MySQL-[Err] 1055 - Expression #1

    © 版权声明:本文为博主原创文章,转载请注明出处 问题描述:在MySQL数据库下,执行SQL插入语句报错.错误信息如下: 错误原因:在MySQL5.7之后,sql_mode中默认存在ONLY_FULL ...

  10. Linux系统运维之路

    九月份开始,半年内搞定运维,博客会慢慢的更新,vim编辑器,Nginx配置文件优化 运维基础 运维基础-Linux发展史.安装.基本操作 运维基础-用户和组管理 运维基础-文件权限管理 运维基础-进程 ...