| Total Complexity | 16 |
| Total Lines | 43 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 9 | class ValidateBinarySearchTree |
||
| 10 | { |
||
| 11 | public static function isValidBST(TreeNode $root): bool |
||
| 14 | } |
||
| 15 | |||
| 16 | public static function isValidBST2(TreeNode $root): bool |
||
| 17 | { |
||
| 18 | if ($root === null) { |
||
| 19 | return true; |
||
| 20 | } |
||
| 21 | [$stack, $prev] = [[], null]; |
||
| 22 | while ($root !== null || $stack) { |
||
| 23 | while ($root !== null) { |
||
| 24 | $stack[] = $root; |
||
| 25 | $root = $root->left; |
||
| 26 | } |
||
| 27 | $root = array_pop($stack); |
||
| 28 | if ($prev !== null && $prev instanceof TreeNode && $root->val <= $prev->val) { |
||
| 29 | return false; |
||
| 30 | } |
||
| 31 | $prev = $root; |
||
| 32 | $root = $root->right; |
||
| 33 | } |
||
| 34 | |||
| 35 | return true; |
||
| 36 | } |
||
| 37 | |||
| 38 | private static function helper(TreeNode $root = null, $min = null, $max = null): bool |
||
| 52 | } |
||
| 53 | } |
||
| 54 |