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 ...
随机推荐
- 终端应用变身文件 MD5/SHA1 校验工具
担心下载的文件被恶意篡改?没有找到 Mac 平台文件校验工具?其实 Mac OS X 系统中已经内置了“文件 MD5/SHA1 校验工具”,它就藏身于终端(Terminal)应用中! 打开终端应用,输 ...
- php数据库操作代码
数据库名为reg,表名为member,字段名为username,password,regdate <?php $link=@mysql_connect("localhost" ...
- [资料分享]GIS+=地理信息+云计算+大数据+容器+物联网+...论文、会议、讲座资料分享
分享地址 http://pan.baidu.com/s/1gesDSB5 部分内容截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...
- foreach_break 面试记录
版权所有@foreach_break] [博客地址 http://www.cnblogs.com/foreach-break] 可以转载,但必须注明出处并保持博客超链接 背景 自从2013年离开北京后 ...
- 香蕉派(or 皮?)上手初体验 -- 外观鉴赏,安装,配置&总结
一.前言及简单介绍 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG9uZ2Vyem9uZQ==/font/5a6L5L2T/fontsize/400/f ...
- 数据结构之---C语言实现图的数组(邻接矩阵)存储表示
//图的数组(邻接矩阵)存储表示 #include <stdio.h> #include <stdlib.h> #define MAX_VEX_NUM 50 typedef c ...
- 【SQLServer】Microsoft SQL Baseline Checklist
今天调查了Microsoft SQL Baseline Checklist中的下面几个问题. Hide Instances Extended Store Procedures Maximum Numb ...
- Shell脚本之:数组
bash支持一维数组,并且没有限定数组的大小,数组元素的下标由0开始编号. 定义数组 在Shell中,用括号来表示数组,数组元素用“空格”符号分割开.定义数组的一般形式为: array_name=(v ...
- 广告banner:手动滑动切换,自动切换,点击跳转,异步加载网络图片
效果图: 该banner功能有自动切换图片,点击图片可以自定义事件,手动滑动切换,异步加载图片 代码说话: 布局文件: <!-- 广告位 --> <FrameLayout andro ...
- 结构体定义:struct与typedef struct
https://blog.csdn.net/haiou0/article/details/6877718?tdsourcetag=s_pcqq_aiomsg https://blog.csdn.net ...