<?phpClass TreeNode { /** @var int $val */ public $val = 0; /** @var TreeNode $left */ public $left; /** @var TreeNode $right */ public $right; public function __construct($val) { $this->val = $val; }}class Solution { public array $res = []; // 是否二叉搜索树 public function isValidBST($root) { return $this->valid($root, null, null); } /** * 递归判断是否二叉搜索树 * 左<中<右 * @param $root * @param $min * @param $max * @return bool */ private function valid($root, $min, $max) { if (is_null($root)) return true; if (!is_null($min) && $root->val <= $min) return false; if (!is_null($max) && $root->val >= $max) return false; return $this->valid($root->left, $min, $root->val) && $this->valid($root->right, $root->val, $max); }}/** * 4 * / \ * 2 6 * / \ / * 1 3 5 */$tree = new TreeNode(4);$tree->left = new TreeNode(2);$tree->left->left = new TreeNode(1);$tree->left->right = new TreeNode(3);$tree->right = new TreeNode(6);$tree->right->left = new TreeNode(5);$cls = new Solution();$r = $cls->isValidBST($tree);echo $r ? 1 : 0;