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 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的更多相关文章
- (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 ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 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 ...
- [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 ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- 20145235 《Java程序设计》第5周学习总结
教材学习内容总结 8.1语法与继承架构 try和catch语法,如果被try{}的语句出现了catch()的问题就执行catch{}的语句. 错误的对象都继承于java.long.Throwable, ...
- w_click_twice
var w_global_obj; var dat = new Date(); var w_golbal_count_millseconds; function w_click_twice(w_cur ...
- P1970 花匠
状态定义是dp中非常重要的,可以直接影响到效率,如此题,第一种思路是: #include <bits/stdc++.h> using namespace std; const int ma ...
- git 第一次初始化
Command line instructions Git global setup git config --global user.name "{名字}({工号})" git ...
- Squid 操作实践
Squid简介 Squid可以做什么 性能要素 Squid安装 Squid快速体验 Squid配置 Squid简介 Squid is a caching proxy for the Web suppo ...
- UDF
一:UDF 1.自定义UDF 二:UDAF 2.UDAF 3.介绍AbstractGenericUDAFResolver 4.介绍GenericUDAFEvaluator 5.程序 package o ...
- Java中重点关键词的区分
1.final, finally, finalize的区别final-修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承. 因此一个类不能既被声明为 abs ...
- NodeJS模块的使用
在NodeJS中,每个js文件就是一个模块,而文件路径就是模块名, 在编写每个模块时,都有require.exports.module三个预先定义好的变量可供使用. require函数用于在当前模块中 ...
- java 发送http json请求
public void getRemoteId(HttpServletRequest request,Model model){ String name = request.getParameter( ...
- Oracle横向纵向汇总
Oracle横向纵向汇总 有一张表test 如下, (NO 学生编号 ,cj 成绩) NO name KM CJ 001 张三 语文 80 001 张三 数学 86 001 张三 英语 75 0 ...