98. Verify binary search tree
Problem-solving ideas
- Modified post-order traversal algorithm. Post-order traversal can carry parameters for traversal.
- The subtree nodes with root as the root node must satisfy max.val > root.val > min.val
- What needs to be done for a node
- If the node is empty, directly true
- If a node value is less than Min false
- If a node value is greater than max false
- Then the postorder traversal algorithm performs recursion
class Solution {
public boolean isV(TreeNode root, TreeNode min,TreeNode max){
if(root == null)
{
return true;
}
if(min != null && root.val <= min.val){
return false;
}
if(max != null && root.val >= max.val){
return false;
}
return isV(root.left,min,root) && isV(root.right,root,max);
}
public boolean isValidBST(TreeNode root) {
return isV(root,null,null);
}
}