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) ...
随机推荐
- (DP)House Robber
题目: You are a professional robber planning to rob houses along a street. Each house has a certain am ...
- ./scripts/feeds update -a OpenWrt大招系列
./scripts/feeds update -a Updating feed 'packages' from 'https://github.com/openwrt/packages.git' .. ...
- MSDN中HttpWebRequest/HttpWebResponse用法
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://home.cnblogs.com/u/weiweiboqi/ ...
- LinQ to SQL 增,删,改 代码演示
NorthwindDBDataContext dc = new NorthwindDBDataContext(); protected void Page_Load(object sender, Ev ...
- 用JUnit4进行参数化测试
参数化测试是一个JUnit 3不具备的功能. 基本使用方法 @RunWith 当类被@RunWith注解修饰,或者类继承了一个被该注解修饰的类,JUnit将会使用这个注解所指明的运行器(runner) ...
- 用Less循环生成样式
需求是这样的,我要给一个轮播图设置不同的背景图,由于每张图片的背景图路劲都不一样,所以需要对每个单独的元素自定义图片路径.然后想到Less语法有mixin机制,就这样实现了一个递归function,然 ...
- 智能路由——ESB
SOA之我见 SOA已然是企业级开发的必定之路.有人会问:我们有了OOP,还须要SOA吗?好吧我承认,这个问题也困扰了我非常久.现现在我得出的结论是:OOP是OOP,SOA是SOA. OOP是指面向对 ...
- request.getRequestURI()与request.getRequestURL()
request.getRequestURL() 获得 http://www.quanqiuyouhui.com/ds-api-test/authorization/test.do request.ge ...
- 关于js中select的简单操作,以及js前台计算,span简单操作
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- flashback database操作步骤
默认情况数据库的flashback database是关闭的. 启用Flashback Database 步骤:1.配置Flash Recovery Area 检查是否启动了flash recover ...