Java for LeetCode 098 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.
解题思路:
本题方法多多,可以采用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的更多相关文章
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- [LeetCode]题解(python):098 Validate Binary Search Tree
题目来源 https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 【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 ...
- 【题解】【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 ...
随机推荐
- 【Maven】3.使用IntelliJ IDEA 使用本地搭建的maven私服,而不是使用默认的maven设置
安装Idea的教程:http://www.cnblogs.com/sxdcgaq8080/p/7641379.html 搭建maven私服的教程:http://www.cnblogs.com/sxdc ...
- 学习PHP:PHP提取的时间出现不准确
php函数date("Y-n-d H-i-s"); 输出的时间与当地时间居然相差了8个小时. 原因是从php5.1.0开始,php.ini里加入了date.time ...
- 【GLSL教程】(九)其他说明 【转】
http://blog.csdn.net/racehorse/article/details/6664775 法线矩阵 在很多顶点shader中都用到了gl_NormalMatrix.这里将介绍这个矩 ...
- Jboss7类载入器
1. 类载入器理论知识介绍 类载入器基于Jboss Module,代替了层次类载入环境,避免了当类存在多个版本号时,导致类载入错误. 类载入是基于模块的.必须显示的定义模块依赖.部署也是模块化的,假设 ...
- TensorFlow笔记四:从生成和保存模型 -> 调用使用模型
TensorFlow常用的示例一般都是生成模型和测试模型写在一起,每次更换测试数据都要重新训练,过于麻烦, 以下采用先生成并保存本地模型,然后后续程序调用测试. 示例一:线性回归预测 make.py ...
- 2016.7.12 去除mybatis-generator生成的class里的注释
用mybatis-generator自动生成代码会出现很多没必要的注释. 在配置文件里加上: 是否去除所有自动生成的文件的时间戳: 是否去除所有自动生成文件的注释: <commentGe ...
- js 查找一串字符串中一段字符
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AutoCAD如何快速标注零件序号
1 先画好一条直线和一个数字 2 选中刚才绘制的数字和直线,选择阵列(估计大概要画四十个就阵列四十行,改一下行偏移) 预览效果如图所示 随后不断重复直线即可 横向也是一样 最后双击 ...
- centos DHCP
yum install dhcp cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example > /etc/dhcp/dhcpd.conf vim /etc ...
- 雕刻效果的实现【OpenCV+QT】
雕刻能够区分为凸雕和凹雕. 凸雕基右下角的点减去左上角的点. 凹雕是左上角的点减去右下角的点. [效果图] 由于进行了缩放.效果看起来差一些.