[LeetCode] 98. Validate Binary Search Tree
作者:互联网
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid 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.
Example1:
Input: root = [2,1,3]
Output: true
Example2:
Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.
Constraints:
- The number of nodes in the tree is in the range [1, 104].
- -231 <= Node.val <= 231 - 1
所谓的搜索二叉树,就是一颗二叉树上每个子树的左子树的值都小于它,它的右子树都数值都大于它。根据这个定义,我们可以想到两个方法。
方法一:
二叉树中序遍历(左根右), 得到一个数组,这个数组单调递增,那么根据定义,就一定是搜索二叉树。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
List<Integer> rst = new ArrayList<>();
helper(root, rst);
return isIncrease(rst);
}
private void helper(TreeNode root, List<Integer> rst) {
if (root == null) {
return;
}
helper(root.left, rst);
rst.add(root.val);
helper(root.right, rst);
}
private boolean isIncrease(List<Integer> rst) {
for (int i = 0; i < rst.size() - 1; i++) {
if (rst.get(i) >= rst.get(i + 1)) {
return false;
}
}
return true;
}
}
第二个思路,就是用基本的递归。但是因为需要判断每层左子树是不是都小于根,右子树是不是都大于根,所以我们需要三个信息。
1)是不是平衡 => 左边的子树是不是平衡+右边子树是不是平衡+该层是不是平衡
2)左子树的最大值 => 只要最大值小于根就所有都小于
3)右子树的最小值 => 只要最小值大于根就所有都大于
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Info {
int leftMax;
int rightMin;
boolean isValid;
Info(int leftMax, int rightMin, boolean isValid) {
this.leftMax = leftMax;
this.rightMin = rightMin;
this.isValid = isValid;
}
}
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
Info rst = helper(root);
return rst.isValid;
}
private Info helper(TreeNode root) {
if (root == null) {
return null;
}
if (root.left == null && root.right == null) {
return new Info(root.val, root.val, true);
}
Info l = helper(root.left);
Info r = helper(root.right);
int leftMax = root.val;
int rightMin = root.val;
boolean isValid = true;
if ( l != null) {
isValid = l.leftMax < root.val && l.isValid && isValid;
leftMax = Math.max(l.leftMax, leftMax);
rightMin = Math.min(l.rightMin, rightMin);
}
if (r != null) {
isValid = r.rightMin > root.val && r.isValid && isValid;
rightMin = Math.min(r.rightMin, rightMin);
leftMax = Math.max(r.leftMax, leftMax);
}
return new Info(leftMax, rightMin, isValid);
}
}
标签:Binary,Search,right,TreeNode,val,int,Tree,rightMin,root 来源: https://www.cnblogs.com/codingEskimo/p/15780168.html