Validate Binary Search Tree

题目:

Given a binary tree, determine if it is a valid Binary Search Tree (BST)

解答:

 1 public class Solution {
 2 
 3     public boolean isValidBST(TreeNode root) {
 4         return valid(root, null, null);
 5     }
 6 
 7     private boolean valid(TreeNode p, Integer low, Integer high) {
 8         if(p == null) {
 9             return true;
10         }
11 
12         return (low == null || p.val > low) && (high == null || p.val < high)
13                 && valid(p.left, low, p.val) && valid(p.right, p.val, high);
14     }   
15 
16 }

 

原文地址:https://www.cnblogs.com/wylwyl/p/10418718.html