Validate Binary Search Tree 解答
Question
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.
Solution 1 -- Recursive
According to the question, we can write recursive statements. Note here whole left/right subtree should be smaller/greater than the root.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null)
return true;
if (root.left != null && !smallerThanRoot(root, root.left))
return false;
if (root.right != null && !greaterThanRoot(root, root.right))
return false;
if (isValidBST(root.left) && isValidBST(root.right))
return true;
return false;
} private boolean greaterThanRoot(TreeNode root, TreeNode child) {
if (child.val <= root.val)
return false;
if (child.left != null) {
if (!greaterThanRoot(root, child.left))
return false;
}
if (child.right != null) {
if (!greaterThanRoot(root, child.right))
return false;
}
return true;
} private boolean smallerThanRoot(TreeNode root, TreeNode child) {
if (child.val >= root.val)
return false;
if (child.left != null) {
if (!smallerThanRoot(root, child.left))
return false;
}
if (child.right != null) {
if (!smallerThanRoot(root, child.right))
return false;
}
return true;
}
}
Solution 2 -- Inorder Traversal
Inorder traversal of BST is an ascending array. Java Stack
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
// This problem can be looked as inorder traversal problem
// Inorder traversal of BST is an ascending array
List<Integer> inOrderResult = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode tmp = root;
while (tmp != null || !stack.empty()) {
if (tmp != null) {
stack.push(tmp);
tmp = tmp.left;
} else {
TreeNode current = stack.pop();
inOrderResult.add(current.val);
tmp = current.right;
}
}
// Traverse list
if (inOrderResult.size() < 1)
return true;
int max = inOrderResult.get(0);
for (int i = 1; i < inOrderResult.size(); i++) {
if (inOrderResult.get(i) > max)
max = inOrderResult.get(i);
else
return false;
}
return true;
}
}
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) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- CF 584B Kolya and Tanya
题目大意:3n个人围着一张桌子,给每个人发钱,可以使1块.2块.3块,第i个人的金额为Ai.若存在第个人使得Ai + Ai+n + Ai+2n != 6,则该分配方案满足条件,求所有的满足条件的方案数 ...
- Hive 9、Hive 在表中添加正则匹配
在Hive中还有一项比较好用的功能,也是非常重要的功能:在建表的时候可以不指定表的行.字段.列的分隔方式,通过给表指定一段正则表达式,让Hive自动去匹配: 1.创建表 CREATE TABLE ap ...
- php 站内搜索 多表 分页
借鉴了:http://blog.chinaunix.net/uid-20787846-id-3488253.html 这篇文章 ,在此基础上增加了分页功能 <?php /* 关键字 */ $ke ...
- Java与面向对象
一.面向过程的思想和面向对象的思想 面向对象和面向过程的思想有着本质上的区别, 作为面向对象的思维来说,当你拿到一个问题时,你分析这个问题不再是第一步先做什么,第二步再做什么,这是面向过程的思维,你应 ...
- Android声明和使用权限
Android定义了一种权限方案来保护设备上的资源和功能.例如,在默认情况下,应用程序无法访问联系人列表.拨打电话等.下面就以拨打电话为例介绍一下系统对权限的要求.一般在我们的应用中,如果要用到拨打电 ...
- Apache POI组件操作Excel,制作报表(一)
Apache的POI组件是Java操作Microsoft Office办公套件的强大API,其中对Word,Excel和PowperPoint都有支持,当然使用较多的还是Excel,因为Word和Po ...
- cocos2d-x 3.0rc开发指南:Windows下Android环境搭建
安装工具 1. 配置JDK JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 本人的系统是Win7 ...
- [转]轻量级 Java Web 框架架构设计
工作闲暇之余,我想设计并开发一款轻量级 Java Web 框架,看看能否取代目前最为流行的而又越来越重的 Spring.Hibernate 等框架.请原谅在下的大胆行为与不自量力,本人不是为了重造轮子 ...
- boost 无锁队列
一哥们翻译的boost的无锁队列的官方文档 原文地址:http://blog.csdn.net/great3779/article/details/8765103 Boost_1_53_0终于迎来了久 ...
- Qt快速入门系列教程目录
Qt快速入门系列教程目录