【判断二叉搜索树】Validate Binary Search Tree
2014-03-12 23:06
447 查看
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.
题意:判断给定的二叉树是否为二叉搜索树
递归解法:使用中序遍历,通过设定的pre节点值和当前遍历值相比较,若符合二叉搜索树的定义则返回真
解法二:非递归实现中序遍历,并进行pre和当前遍历值的判断
public class Solution {
public boolean inorder(TreeNode root){
if(root == null) return true;
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode p = root;
while(p!=null || !s.isEmpty()){
while(p!=null){
s.push(p);
p = p.left;
}
if(!s.isEmpty()){
TreeNode t = s.pop();
if(pre!=null && pre.val>=t.val) return false;
pre = t;
p = t.right;
}
}
return true;
}
public boolean isValidBST(TreeNode root) {
return inorder(root);
}
}
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.
题意:判断给定的二叉树是否为二叉搜索树
递归解法:使用中序遍历,通过设定的pre节点值和当前遍历值相比较,若符合二叉搜索树的定义则返回真
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private TreeNode pre=null; public boolean inorder(TreeNode root){ if(root == null) return true; boolean left = inorder(root.left); if(pre != null && pre.val>=root.val) return false; pre = root; boolean right = inorder(root.right); return left && right; } public boolean isValidBST(TreeNode root) { return inorder(root); } }
解法二:非递归实现中序遍历,并进行pre和当前遍历值的判断
public class Solution {
public boolean inorder(TreeNode root){
if(root == null) return true;
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode p = root;
while(p!=null || !s.isEmpty()){
while(p!=null){
s.push(p);
p = p.left;
}
if(!s.isEmpty()){
TreeNode t = s.pop();
if(pre!=null && pre.val>=t.val) return false;
pre = t;
p = t.right;
}
}
return true;
}
public boolean isValidBST(TreeNode root) {
return inorder(root);
}
}
相关文章推荐
- Validate Binary Search Tree 二叉搜索树的合法判断
- 判断给定二叉树是否是二叉搜索树(LeetCode: Validate Binary Search Tree)
- LeetCode(Validate Binary Search Tree) 判断一个二叉树是否是二叉搜索树
- LeetCode 98 Validate Binary Search Tree(判断二叉搜索树)
- [leetcode]Validate Binary Search Tree (判断有效二叉搜索树 C语言实现)
- LeetCode 98 Validate Binary Search Tree判断是否为合法二叉树
- Leetcode:Validate Binary Tree 判断一个二叉树是否为二叉搜索树
- 判断二叉查找树的合法性 Validate Binary Search Tree
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
- LeetCode-Validate Binary Search Tree-合法二叉搜索树-二叉树递归
- 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
- 41. leetcode98. Validate Binary Search Tree【验证二叉搜索树】
- LeetCode--Validate Binary Search Tree(二叉搜索树)
- 【LeetCode】Validate Binary Search Tree 二叉查找树的判断
- LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)
- LeetCode | Validate Binary Search Tree(有效的二叉搜索树)
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
- Validate Binary Search Tree,判断是否是二叉排序树
- 【LeetCode-面试算法经典-Java实现】【098-Validate Binary Search Tree(验证二叉搜索树)】
- Validate Binary Search Tree--判断一个树是不是二叉查找树(重重重)