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) ...
随机推荐
- HDU 1394 Minimum Inversion Number(线段树 或 树状数组)
题目大意:给出从 0 到 n-1 的整数序列,A0,A1,A2...An-1.可将该序列的前m( 0 <= m < n )个数移到后面去,组成其他的序列,例如当 m=2 时,得到序列 A2 ...
- java中File类的相关学习
File类 1.关于系统路径分割符. 在Windows中,使用反斜杠“\”作为路径分割符,比如“c:\test”,但是java中反斜杠表示转义,所以需要用“C:\\test”在程序中来表示路径.还可以 ...
- The Frog's Games(二分)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- 【Struts2】新建一个Struts2工程,初步体验MVC
实现目标 地址栏输入http://localhost:88/Struts2HelloWorld/helloworld.jsp 输入用户名,交由http://localhost:88/Struts2He ...
- NET中级课--文件,流,序列化3
1.序列化:将对象及状态保存起来. 反序列化就是逆操作. 2.NET提供了一个接口:System.runtime.serialization.IFormatter接口, 还有实现了这个接口的类Bina ...
- js图片放大镜 可动态更换图片
现仅已.NET为例,HTML代码如下 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > & ...
- (转) 学习C++ -> 引用( References )
学习C++ -> 引用( References ) 一.引用的介绍 引用就是某一变量(目标)的一个别名, 相当于同一个人有了两个名字, 无论喊哪一个名字实际上都是指的同一个人. 同样, 在 ...
- (原)Opencv中直方图均衡和图像动态范围拉伸的代码
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5102032.html 参考网址: http://blog.csdn.net/abcjennifer/a ...
- 修改mysql编码为UTF-8
mysql> show variables like '%character%'; +--------------------------+--------------------------- ...
- jquery获取当前鼠标所在位置的坐标
$(document).ready(function(){ $(document).mousemove(function(e){ $('#xy').html("X :"+e.pag ...