LeetCode OJ: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.
一开始是这样做的:
class Solution {
public:
bool isValidBST(TreeNode* root) {
return checkValid(root, INT_MIN, INT_MAX);
}
bool checkValid(TreeNode * root, int left, int right)
{
if(root == NULL)
return true;
return(root->val > left && root->val < right && checkValid(root->left, left, root->val) ]
&& checkValid(root->right, root->val, right));
}
};
然而并不能通过,因为leetCode增加了两个新的测试用例,把INT_MAX以及INT_MIN也囊括进去了。
那么就只好用中序遍历的方法来了(一开始想的是先中序遍历一遍,然后根据遍历的到的值是不是升序排列的来判断这个二叉树是不是BST,但是后来发现传入一个引用的参数可以实现一边递归一边比较),代码如下:
class Solution{
public:
bool isValidBST(TreeNode* root) {
TreeNode * helper = NULL;
return inOrder(root, helper);
}
bool inOrder(TreeNode * root, TreeNode * & prev){//注意这里的是引用
if(root == NULL)
return true;
bool left = inOrder(root->left, prev);
if(prev != NULL && prev->val >= root->val)
return false;
prev = root;
bool right = inOrder(root->right, prev);
return left && right;
}
};
当然上面的也可以采用迭代的方式来做:
class Solution {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode *> s;
TreeNode * pre = NULL;
if(root == NULL)
return true;
while(root || !s.empty()){
while(root != NULL){
s.push(root);
root = root->left;
}
root = s.top();
s.pop();
if(pre != NULL && root->val <= pre->val) return false;
pre = root;
root = root->right;
}
return true;
}
};
下面是java版本的代码,首先还是不可行的Integer.MAX_VALUE方法,但是还是贴下吧:
/**
* 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) {
return checkValid(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
} boolean checkValid(TreeNode root, int left, int right){
if(root == null)
return true;
if(root.val <= left || root.val >= right){
return false;
}
return checkValid(root.left, left, root.val)
&& checkValid(root.right, root.val, right);
}
}
那么只有遍历树了,然后来判断, 这里使用非递归的方法来写:
public class Solution {
public boolean isValidBST(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
HashMap<TreeNode, Integer> map = new HashMap<TreeNode, Integer>();
List<Integer> list = new ArrayList<Integer>();
if(root == null)
return true;
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.peek();
while(node.left != null && !map.containsKey(node.left)){
stack.push(node.left);
map.put(node.left, 1);
node = node.left;
}
stack.pop();
list.add(node.val);
if(node.right != null && !map.containsKey(node.right)){
stack.push(node.right);
map.put(node.right, 1);
}
}
for(int i = 0; i < list.size() - 1; ++i){
if(list.get(i) >= list.get(i+1))
return false;
}
return true;
}
}
LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)的更多相关文章
- [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]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 ...
- [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 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [leetcode]99. Recover Binary Search Tree恢复二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 098 Validate Binary Search Tree 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义: 左子树只包含小于当前节点的数. 右子树只包含大于当前节点的数. 所有子树自身必须也是二叉搜索树.示例 1 ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
随机推荐
- nodejs中全栈开发框架meteor的文档
http://wiki.jikexueyuan.com/project/discover-meteor/routing.html, 这本书的源码地址: https://github.com/Dis ...
- payload有效载荷(转)
payload 记载着信息的那部分数据.通常在传输数据时,为了使数据传输更可靠,要把原始数据分批传输,并且在每一批数据的头和尾都加上一定的辅助信息,比如这一批数据量的大小,校验位等,这样就相当于给已经 ...
- django admin基础
通过onetoonefiled扩展得到的不会在添加user是自动添加原因是onetoonefiled只是一个model 可以they are just Django models that happe ...
- SQL SREVER, ORACLE数据库连接字符串
采用windows身份验证模式 Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
- OJ 1101 谁是中间的那个
前言:主要考察排序用法 sort(cow+1,cow+1+n,cmp);//数组按cmp方法排序 Description 一天,农夫乔伊像往常一样来到了他的牧场,他突然对他的奶牛产奶量产生了兴趣.他想 ...
- Cookie用法简介
java操作Cookie---javax.servlet.http.Cookie 1.增加一个Cookie Cookie cookie = new Cookie("username" ...
- Sourse Insight使用过程中的常使用功能简介
1.查找定义: 在编辑区内,选中任意一个变量.函数.宏等,点击右键->Jump Definition,或者直接Ctrl+左击 2.编辑区切换
- keepalived nginx 主备配置
keepalived nginx 主备配置(多主多备同理) 1.Nginx服务安装 nginx 不区分主备,在两台服务上安装两个即可. 安装参考:https://www.cnblogs.com/zw ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- /usr/lib/update-notifier/updates-available
/usrb/update-notifier/update-motd-updates-available: 49: /usrb/update-notifier/update-motd-updates- ...