|
4 | 4 | * int val;
|
5 | 5 | * TreeNode left;
|
6 | 6 | * TreeNode right;
|
7 |
| - * TreeNode(int x) { val = x; } |
| 7 | + * TreeNode() {} |
| 8 | + * TreeNode(int val) { this.val = val; } |
| 9 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 10 | + * this.val = val; |
| 11 | + * this.left = left; |
| 12 | + * this.right = right; |
| 13 | + * } |
8 | 14 | * }
|
9 | 15 | */
|
10 | 16 | classSolution {
|
11 | 17 | publicbooleanisValidBST(TreeNoderoot) {
|
12 |
| -longmin = (long)Integer.MIN_VALUE; |
13 |
| -longmax = (long)Integer.MAX_VALUE; |
14 |
| -returnhelper(root,min -1,max +1); |
| 18 | +returnisValidBSTHelper(root, (((long)Integer.MAX_VALUE) +1), ((long)Integer.MIN_VALUE) -1); |
15 | 19 | }
|
16 | 20 |
|
17 |
| -privatebooleanhelper(TreeNoderoot,longmin,longmax) { |
18 |
| -if (root ==null) { |
| 21 | +privatebooleanisValidBSTHelper(TreeNodenode,longmax,longmin) { |
| 22 | +if (node ==null) { |
19 | 23 | returntrue;
|
20 | 24 | }
|
21 |
| -if (root.val<=min ||root.val>=max) { |
| 25 | +if (node.val>=max ||node.val<=min) { |
22 | 26 | returnfalse;
|
23 | 27 | }
|
24 |
| -returnhelper(root.left,min, (long)root.val)&&helper(root.right,(long)root.val,max); |
| 28 | +returnisValidBSTHelper(node.left,node.val,min)&&isValidBSTHelper(node.right,max,node.val); |
25 | 29 | }
|
26 | 30 | }
|