LeetCode098 Validate Binary Search Tree
2017-04-25 21:35
465 查看
详细见:leetcode.com/problems/validate-binary-search-tree
Java Solution:
github
package leetcode;
import java.util.LinkedList;
import java.util.Queue;
/*
* 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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
*/
import tools.TreeNode辅助.TreeNode;;
public class P098_ValidateBinarySearchTree {
static int N = Integer.MIN_VALUE;
public static void main(String[] args) {
TreeNode root = tools.TreeNode辅助.A_生成满二叉树(new int[] {
10,
5, 15,
N, N, 6, 20
});
root = tools.TreeNode辅助.A_生成满二叉树(new int[] {
10,
5, 15,
N, N, 12, 20
});
Solution2 s = new Solution2();
System.out.println(s.isValidBST(root));
tools.TreeNode辅助.B_按层打印(root);
}
/*
* 这种想法太简单了,肯定是错误的。
*/
static class Solution {
public boolean isValidBST(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (! queue.isEmpty()) {
TreeNode root_now = queue.poll();
if (root_now.left != null) {
if (root_now.left.val > root_now.val) {
return false;
}
queue.add(root_now.left);
}
if (root_now.right != null) {
if (root_now.right.val < root_now.val) {
return false;
}
queue.add(root_now.right);
}
}
return true;
}
}
/*
* AC
* 0 ms
*/
static class Solution2 {
boolean isFalse = false;
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
search(root, Long.MAX_VALUE, Long.MIN_VALUE);
return ! isFalse;
}
/*
* 保证root != null
*/
void search(TreeNode root, long max_edge, long min_edge) {
if (isFalse) {
return;
}
if ((long) root.val >= max_edge || (long) root.val <= min_edge) {
isFalse = true;
return;
}
if (root.left != null) {
search(root.left, root.val, min_edge);
}
if (root.right != null) {
search(root.right, max_edge, root.val);
}
}
}
}
C Solution:
github
/*
url: leetcode.com/problems/validate-binary-search-tree
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define bool int
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
long _min(long a, long b) {
return a < b ? a : b;
}
long _max(long a, long b) {
return a > b ? a : b;
}
bool search(struct TreeNode* root, long min, long max) {
long v = 0;
if (root == NULL) return 1;
v = root->val;
if (v < min || v > max) return 0;
//printf("%ld %ld\n", min, max);
return search(root->left, min, _min(max, v-1)) &&
search(root->right, _max(min, v+1), max);
}
bool isValidBST(struct TreeNode* root) {
return search(root, INT_MIN, INT_MAX);
}
Python Solution:
github
#coding=utf-8
'''
url: leetcode.com/problems/validate-binary-search-tree
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年4月25日
@details: Solution: 82ms 51.50%
'''
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def search(self, n, i, j):
if n == None: return True
if n.val > j or n.val < i: return False
return self.search(n.left, i, min(j, n.val-1)) \
and self.search(n.right, max(i, n.val+1), j)
def isValidBST(self, n):
"""
:type n: TreeNode
:rtype: bool
"""
if n == None: return True
return self.search(n, -2147483648, 2147483647)
Java Solution:
github
package leetcode;
import java.util.LinkedList;
import java.util.Queue;
/*
* 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.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.
*/
import tools.TreeNode辅助.TreeNode;;
public class P098_ValidateBinarySearchTree {
static int N = Integer.MIN_VALUE;
public static void main(String[] args) {
TreeNode root = tools.TreeNode辅助.A_生成满二叉树(new int[] {
10,
5, 15,
N, N, 6, 20
});
root = tools.TreeNode辅助.A_生成满二叉树(new int[] {
10,
5, 15,
N, N, 12, 20
});
Solution2 s = new Solution2();
System.out.println(s.isValidBST(root));
tools.TreeNode辅助.B_按层打印(root);
}
/*
* 这种想法太简单了,肯定是错误的。
*/
static class Solution {
public boolean isValidBST(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (! queue.isEmpty()) {
TreeNode root_now = queue.poll();
if (root_now.left != null) {
if (root_now.left.val > root_now.val) {
return false;
}
queue.add(root_now.left);
}
if (root_now.right != null) {
if (root_now.right.val < root_now.val) {
return false;
}
queue.add(root_now.right);
}
}
return true;
}
}
/*
* AC
* 0 ms
*/
static class Solution2 {
boolean isFalse = false;
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
search(root, Long.MAX_VALUE, Long.MIN_VALUE);
return ! isFalse;
}
/*
* 保证root != null
*/
void search(TreeNode root, long max_edge, long min_edge) {
if (isFalse) {
return;
}
if ((long) root.val >= max_edge || (long) root.val <= min_edge) {
isFalse = true;
return;
}
if (root.left != null) {
search(root.left, root.val, min_edge);
}
if (root.right != null) {
search(root.right, max_edge, root.val);
}
}
}
}
C Solution:
github
/*
url: leetcode.com/problems/validate-binary-search-tree
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define bool int
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
long _min(long a, long b) {
return a < b ? a : b;
}
long _max(long a, long b) {
return a > b ? a : b;
}
bool search(struct TreeNode* root, long min, long max) {
long v = 0;
if (root == NULL) return 1;
v = root->val;
if (v < min || v > max) return 0;
//printf("%ld %ld\n", min, max);
return search(root->left, min, _min(max, v-1)) &&
search(root->right, _max(min, v+1), max);
}
bool isValidBST(struct TreeNode* root) {
return search(root, INT_MIN, INT_MAX);
}
Python Solution:
github
#coding=utf-8
'''
url: leetcode.com/problems/validate-binary-search-tree
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年4月25日
@details: Solution: 82ms 51.50%
'''
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def search(self, n, i, j):
if n == None: return True
if n.val > j or n.val < i: return False
return self.search(n.left, i, min(j, n.val-1)) \
and self.search(n.right, max(i, n.val+1), j)
def isValidBST(self, n):
"""
:type n: TreeNode
:rtype: bool
"""
if n == None: return True
return self.search(n, -2147483648, 2147483647)
相关文章推荐
- leetcode 098 Validate Binary Search Tree
- [LeetCode]题解(python):098- Validate Binary Search Tree
- LeetCode 098 Validate Binary Search Tree
- 098 Validate Binary Search Tree [Leetcode]
- LeetCode: Validate Binary Search Tree [098]
- Java for LeetCode 098 Validate Binary Search Tree
- leetcode_098 Validate Binary Search Tree
- 【LeetCode-面试算法经典-Java实现】【098-Validate Binary Search Tree(验证二叉搜索树)】
- LeetCode 098 Validate Binary Search Tree
- leetcode_c++:树:Validate Binary Search Tree(098)
- LeetCode Validate Binary Search Tree
- 【Leetcode】Validate Binary Search Tree
- [C++]LeetCode: 122 Validate Binary Search Tree (是否是合法BST树)
- leetcode — validate-binary-search-tree
- [LeetCode] Validate Binary Search Tree
- 【leetcode刷题笔记】Validate Binary Search Tree
- Validate Binary Search Tree [LeetCode]
- Validate Binary Search Tree (leetcode)
- LeetCode Validate Binary Search Tree
- Validate Binary Search Tree (leetcode)