From 104f1ff5a1837e059336454318d1c8cc65e1b06b Mon Sep 17 00:00:00 2001 From: yuanguangxin <274841922@qq.com> Date: Sat, 8 Feb 2020 18:13:15 +0800 Subject: [PATCH] update q98 --- .idea/workspace.xml | 120 ++++++++++++++++++--------------- src/q98/{ => f1}/Solution.java | 2 +- src/q98/{ => f1}/TreeNode.java | 2 +- src/q98/f2/Solution.java | 46 ++++++------- src/q98/f3/Solution.java | 58 ++++++++++++++++ src/q98/f3/TreeNode.java | 11 +++ 6 files changed, 158 insertions(+), 81 deletions(-) rename src/q98/{ => f1}/Solution.java (98%) rename src/q98/{ => f1}/TreeNode.java (88%) create mode 100644 src/q98/f3/Solution.java create mode 100644 src/q98/f3/TreeNode.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4fac1db..8d5fa01 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,26 +1,13 @@ - - - - - - - - - - - - - + + + - - - - - - + + + + + - - @@ -95,7 +82,7 @@ - + - + + + - @@ -234,7 +234,7 @@ - + 1580045439607 @@ -313,7 +313,14 @@ - @@ -331,7 +338,8 @@ - @@ -346,66 +354,66 @@ - + - - + + - - + + - - - + + + - - - + + + - - - + + + - - - + + + - + - + - + - + - + - + - + - + - - + + - + diff --git a/src/q98/Solution.java b/src/q98/f1/Solution.java similarity index 98% rename from src/q98/Solution.java rename to src/q98/f1/Solution.java index fba707b..181547e 100644 --- a/src/q98/Solution.java +++ b/src/q98/f1/Solution.java @@ -1,4 +1,4 @@ -package q98; +package q98.f1; import java.util.ArrayList; import java.util.List; diff --git a/src/q98/TreeNode.java b/src/q98/f1/TreeNode.java similarity index 88% rename from src/q98/TreeNode.java rename to src/q98/f1/TreeNode.java index 222e309..5ade815 100644 --- a/src/q98/TreeNode.java +++ b/src/q98/f1/TreeNode.java @@ -1,4 +1,4 @@ -package q98; +package q98.f1; public class TreeNode { int val; diff --git a/src/q98/f2/Solution.java b/src/q98/f2/Solution.java index ca4c92c..e100c58 100644 --- a/src/q98/f2/Solution.java +++ b/src/q98/f2/Solution.java @@ -1,32 +1,32 @@ package q98.f2; -import java.util.Stack; - +/** + * 寻找上下界递归 o(n) + */ public class Solution { - public boolean isValidBST(TreeNode root) { - Stack stack = new Stack<>(); - while (!stack.empty() || root != null) { - while (root != null) { - stack.push(root); - root = root.left; - } - root = stack.pop(); - System.out.println(root.val); - root = root.right; + public boolean valid(TreeNode root, Integer min, Integer max) { + if (root == null) { + return true; + } + int val = root.val; + + if (min != null && val <= min) { + return false; + } + if (max != null && val >= max) { + return false; + } + + if (!valid(root.left, min, val)) { + return false; + } + if (!valid(root.right, val, max)) { + return false; } return true; } - public static void main(String[] args) { - TreeNode root = new TreeNode(5); - TreeNode n1 = new TreeNode(1); - TreeNode n2 = new TreeNode(4); - root.left = n1; - root.right = n2; - TreeNode n3 = new TreeNode(3); - TreeNode n4 = new TreeNode(6); - n2.left = n3; - n2.right = n4; - System.out.println(new Solution().isValidBST(root)); + public boolean isValidBST(TreeNode root) { + return valid(root, null, null); } } diff --git a/src/q98/f3/Solution.java b/src/q98/f3/Solution.java new file mode 100644 index 0000000..7734f5a --- /dev/null +++ b/src/q98/f3/Solution.java @@ -0,0 +1,58 @@ +package q98.f3; + +import java.util.LinkedList; +import java.util.Queue; + +/** + * 层序遍历迭代法判断上下界 o(n) + */ +public class Solution { + public boolean isValidBST(TreeNode root) { + if (root == null) { + return true; + } + Queue queue = new LinkedList<>(); + queue.add(root); + + Queue min = new LinkedList<>(); + Queue max = new LinkedList<>(); + while (!queue.isEmpty()) { + TreeNode temp = queue.poll(); + Integer maxt = max.poll(); + Integer mint = min.poll(); + + if (mint != null && temp.val <= mint) { + return false; + } + if (maxt != null && temp.val >= maxt) { + return false; + } + + if (temp.left != null) { + min.add(mint); + max.add(temp.val); + queue.add(temp.left); + } + + if (temp.right != null) { + max.add(maxt); + min.add(temp.val); + queue.add(temp.right); + } + } + return true; + } + + public static void main(String[] args) { + TreeNode root = new TreeNode(10); + TreeNode n1 = new TreeNode(5); + TreeNode n2 = new TreeNode(15); + root.left = n1; + root.right = n2; + TreeNode n3 = new TreeNode(6); + TreeNode n4 = new TreeNode(20); + n2.left = n3; + n2.right = n4; + System.out.println(new Solution().isValidBST(root)); + } +} \ No newline at end of file diff --git a/src/q98/f3/TreeNode.java b/src/q98/f3/TreeNode.java new file mode 100644 index 0000000..aee1da4 --- /dev/null +++ b/src/q98/f3/TreeNode.java @@ -0,0 +1,11 @@ +package q98.f3; + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +}