-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions
64
src/main/java/com/diguage/algorithm/leetcode/_0098_ValidateBinarySearchTree_2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.diguage.algorithm.leetcode; | ||
|
||
import com.diguage.algorithm.util.TreeNode; | ||
|
||
import java.util.Arrays; | ||
|
||
import static com.diguage.algorithm.util.TreeNodeUtils.buildTree; | ||
|
||
public class _0098_ValidateBinarySearchTree_2 { | ||
public boolean isValidBST(TreeNode root) { | ||
return dfs(root).valid; | ||
} | ||
|
||
private Record dfs(TreeNode node) { | ||
if (node == null) { | ||
return new Record(true, Long.MIN_VALUE, Long.MAX_VALUE); | ||
} | ||
Record left = dfs(node.left); | ||
if (!left.valid) { | ||
return new Record(false, Long.MIN_VALUE, Long.MAX_VALUE); | ||
} | ||
Record right = dfs(node.right); | ||
if (!right.valid) { | ||
return new Record(false, Long.MIN_VALUE, Long.MAX_VALUE); | ||
} | ||
|
||
long max = Math.max(left.max, Math.max(node.val, right.max)); | ||
long min = Math.min(left.min, Math.min(node.val, right.min)); | ||
|
||
return new Record(left.valid && right.valid | ||
&& left.max < node.val && node.val < right.min, max, min); | ||
} | ||
|
||
public static class Record { | ||
public boolean valid; | ||
public long max; | ||
public long min; | ||
|
||
public Record(boolean valid, long max, long min) { | ||
this.valid = valid; | ||
this.max = max; | ||
this.min = min; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
_0098_ValidateBinarySearchTree_2 solution = new _0098_ValidateBinarySearchTree_2(); | ||
|
||
boolean r5 = solution.isValidBST(buildTree(Arrays.asList(2147483647))); | ||
System.out.println(r5); | ||
|
||
boolean r4 = solution.isValidBST(buildTree(Arrays.asList(3, 1, 5, 0, 2, 4, 6, null, null, null, 3))); | ||
System.out.println(r4); | ||
|
||
boolean r3 = solution.isValidBST(buildTree(Arrays.asList(10, 5, 15, null, null, 6, 20))); | ||
System.out.println(r3); | ||
|
||
boolean r1 = solution.isValidBST(buildTree(Arrays.asList(2, 1, 3))); | ||
System.out.println(r1); | ||
|
||
boolean r2 = solution.isValidBST(buildTree(Arrays.asList(5, 1, 4, null, null, 3, 6))); | ||
System.out.println(r2); | ||
} | ||
} |