diff --git a/src/class08_12/Code01_IsCBT.java b/src/class08_12/Code01_IsCBT.java index a5a153e..812b33d 100644 --- a/src/class08_12/Code01_IsCBT.java +++ b/src/class08_12/Code01_IsCBT.java @@ -30,7 +30,9 @@ public class Code01_IsCBT { r = head.right; if ( // 如果遇到了不双全的节点之后,又发现当前节点不是叶节点 - (leaf && (l != null || r != null)) || (l == null && r != null) + (leaf && (l != null || r != null)) + || + (l == null && r != null) ) { return false; diff --git a/src/class08_12/Code02_IsBST.java b/src/class08_12/Code02_IsBST.java index 199c188..6ff2f10 100644 --- a/src/class08_12/Code02_IsBST.java +++ b/src/class08_12/Code02_IsBST.java @@ -45,42 +45,52 @@ public class Code02_IsBST { } public static class Info { - boolean isBST; - public int min; + public boolean isBST; public int max; + public int min; - public Info(boolean is, int mi, int ma) { - isBST = is; - min = mi; + public Info(boolean i, int ma, int mi) { + isBST = i; max = ma; + min = mi; } + } - public static Info process(Node head) { - if (head == null) { + public static Info process(Node x) { + if (x == null) { return null; } - Info leftInfo = process(head.left); - Info rightInfo = process(head.right); - int min = head.value; - int max = head.value; + Info leftInfo = process(x.left); + Info rightInfo = process(x.right); + int max = x.value; if (leftInfo != null) { - min = Math.min(min, leftInfo.min); max = Math.max(max, leftInfo.max); } if (rightInfo != null) { - min = Math.min(min, rightInfo.min); max = Math.max(max, rightInfo.max); } - boolean isBST = false; - if ( - (leftInfo == null ? true : (leftInfo.isBST && leftInfo.max < head.value)) - && - (rightInfo == null ? true : (rightInfo.isBST && rightInfo.min > head.value)) - ) { - isBST = true; + int min = x.value; + if (leftInfo != null) { + min = Math.min(min, leftInfo.min); + } + if (rightInfo != null) { + min = Math.min(min, rightInfo.min); + } + boolean isBST = true; + if (leftInfo != null && !leftInfo.isBST) { + isBST = false; + } + if (rightInfo != null && !rightInfo.isBST) { + isBST = false; + } + if (leftInfo != null && leftInfo.max >= x.value) { + isBST = false; + } + if (rightInfo != null && rightInfo.min <= x.value) { + isBST = false; } - return new Info(isBST, min, max); + return new Info(isBST, max, min); } // for test diff --git a/src/class08_12/Code03_IsBalanced.java b/src/class08_12/Code03_IsBalanced.java index 0eee9fd..df44cee 100644 --- a/src/class08_12/Code03_IsBalanced.java +++ b/src/class08_12/Code03_IsBalanced.java @@ -32,33 +32,43 @@ public class Code03_IsBalanced { } public static boolean isBalanced2(Node head) { - return process2(head).isBalaced; + return process(head).isBalanced; } - - // 左、右要求一样,Info 信息返回的结构体 - public static class Info { - public boolean isBalaced; + + public static class Info{ + public boolean isBalanced; public int height; - - public Info(boolean b, int h) { - isBalaced = b; + + public Info(boolean i, int h) { + isBalanced = i; height = h; } } - - public static Info process2(Node X) { - if (X == null) { + + public static Info process(Node x) { + if(x == null) { return new Info(true, 0); } - Info leftInfo = process2(X.left); - Info rightInfo = process2(X.right); - int height = Math.max(leftInfo.height, rightInfo.height) + 1; + Info leftInfo = process(x.left); + Info rightInfo = process(x.right); + int height = Math.max(leftInfo.height, rightInfo.height) + 1; boolean isBalanced = true; - if (!leftInfo.isBalaced || !rightInfo.isBalaced || Math.abs(leftInfo.height - rightInfo.height) > 1) { + if(!leftInfo.isBalanced) { + isBalanced = false; + } + if(!rightInfo.isBalanced) { + isBalanced = false; + } + if(Math.abs(leftInfo.height - rightInfo.height) > 1) { isBalanced = false; } return new Info(isBalanced, height); } + + + + + // for test public static Node generateRandomBST(int maxLevel, int maxValue) { diff --git a/src/class08_12/Code05_MaxSubBSTSize.java b/src/class08_12/Code05_MaxSubBSTSize.java index d35e699..db29d17 100644 --- a/src/class08_12/Code05_MaxSubBSTSize.java +++ b/src/class08_12/Code05_MaxSubBSTSize.java @@ -48,154 +48,165 @@ public class Code05_MaxSubBSTSize { return Math.max(maxSubBSTSize1(head.left), maxSubBSTSize1(head.right)); } - public static int maxSubBSTSize2(Node head) { - if (head == null) { - return 0; - } - return process(head).maxSubBSTSize; - } - - - -// public static Info process(Node head) { +// public static int maxSubBSTSize2(Node head) { // if (head == null) { +// return 0; +// } +// return process(head).maxSubBSTSize; +// } +// +// +// +// +// +// +// +// +// +// // 任何子树 +// public static class Info { +// public boolean isAllBST; +// public int maxSubBSTSize; +// public int min; +// public int max; +// +// public Info(boolean is, int size, int mi, int ma) { +// isAllBST = is; +// maxSubBSTSize = size; +// min = mi; +// max = ma; +// } +// } +// +// +// +// +// public static Info process(Node X) { +// if(X == null) { // return null; // } -// Info leftInfo = process(head.left); -// Info rightInfo = process(head.right); -// int min = head.value; -// int max = head.value; -// int maxSubBSTSize = 0; -// if (leftInfo != null) { +// Info leftInfo = process(X.left); +// Info rightInfo = process(X.right); +// +// +// +// int min = X.value; +// int max = X.value; +// +// if(leftInfo != null) { // min = Math.min(min, leftInfo.min); // max = Math.max(max, leftInfo.max); -// maxSubBSTSize = Math.max(maxSubBSTSize, leftInfo.maxSubBSTSize); // } -// if (rightInfo != null) { +// if(rightInfo != null) { // min = Math.min(min, rightInfo.min); // max = Math.max(max, rightInfo.max); +// } +// +// +// +// +// +// +// +// int maxSubBSTSize = 0; +// if(leftInfo != null) { +// maxSubBSTSize = leftInfo.maxSubBSTSize; +// } +// if(rightInfo !=null) { // maxSubBSTSize = Math.max(maxSubBSTSize, rightInfo.maxSubBSTSize); // } -// boolean isBST = false; -// if ((leftInfo == null ? true : (leftInfo.isAllBST && leftInfo.max < head.value)) -// && (rightInfo == null ? true : (rightInfo.isAllBST && rightInfo.min > head.value))) { -// isBST = true; -// maxSubBSTSize = (leftInfo == null ? 0 : leftInfo.maxSubBSTSize) -// + (rightInfo == null ? 0 : rightInfo.maxSubBSTSize) + 1; +// boolean isAllBST = false; +// +// +// if( +// // 左树整体需要是搜索二叉树 +// ( leftInfo == null ? true : leftInfo.isAllBST ) +// && +// ( rightInfo == null ? true : rightInfo.isAllBST ) +// && +// // 左树最大值 X.value) +// +// +// ) { +// +// maxSubBSTSize = +// (leftInfo == null ? 0 : leftInfo.maxSubBSTSize) +// + +// (rightInfo == null ? 0 : rightInfo.maxSubBSTSize) +// + +// 1; +// isAllBST = true; +// +// // } -// return new Info(isBST, maxSubBSTSize, min, max); +// return new Info(isAllBST, maxSubBSTSize, min, max); // } - - - - - - - - - - // 任何子树 + + public static int maxSubBSTSize2(Node head) { + if(head == null) { + return 0; + } + return process(head).maxBSTSubtreeSize; + } + public static class Info { - public boolean isAllBST; - public int maxSubBSTSize; - public int min; + public int maxBSTSubtreeSize; + public int allSize; public int max; + public int min; - public Info(boolean is, int size, int mi, int ma) { - isAllBST = is; - maxSubBSTSize = size; - min = mi; + public Info(int m, int a, int ma, int mi) { + maxBSTSubtreeSize = m; + allSize = a; max = ma; + min = mi; } } - - - - - public static Info process(Node X) { - if(X == null) { + + public static Info process(Node x) { + if (x == null) { return null; } - Info leftInfo = process(X.left); - Info rightInfo = process(X.right); - - - - int min = X.value; - int max = X.value; - - if(leftInfo != null) { - min = Math.min(min, leftInfo.min); - max = Math.max(max, leftInfo.max); - } - if(rightInfo != null) { - min = Math.min(min, rightInfo.min); - max = Math.max(max, rightInfo.max); - } - - - - - - - - int maxSubBSTSize = 0; - if(leftInfo != null) { - maxSubBSTSize = leftInfo.maxSubBSTSize; - } - if(rightInfo !=null) { - maxSubBSTSize = Math.max(maxSubBSTSize, rightInfo.maxSubBSTSize); - } - boolean isAllBST = false; - - - if( - // 左树整体需要是搜索二叉树 - ( leftInfo == null ? true : leftInfo.isAllBST ) - && - ( rightInfo == null ? true : rightInfo.isAllBST ) - && - // 左树最大值 X.value) - - - ) { - - maxSubBSTSize = - (leftInfo == null ? 0 : leftInfo.maxSubBSTSize) - + - (rightInfo == null ? 0 : rightInfo.maxSubBSTSize) - + - 1; - isAllBST = true; - - - } - - - - - - - - - - - - - - - - - - return new Info(isAllBST, maxSubBSTSize, min, max); + Info leftInfo = process(x.left); + Info rightInfo = process(x.right); + int max = x.value; + int min = x.value; + int allSize = 1; + if (leftInfo != null) { + max = Math.max(leftInfo.max, max); + min = Math.min(leftInfo.min, min); + allSize += leftInfo.allSize; + } + if (rightInfo != null) { + max = Math.max(rightInfo.max, max); + min = Math.min(rightInfo.min, min); + allSize += rightInfo.allSize; + } + int p1 = -1; + if (leftInfo != null) { + p1 = leftInfo.maxBSTSubtreeSize; + } + int p2 = -1; + if (rightInfo != null) { + p2 = rightInfo.maxBSTSubtreeSize; + } + int p3 = -1; + boolean leftBST = leftInfo == null ? true : (leftInfo.maxBSTSubtreeSize == leftInfo.allSize); + boolean rightBST = rightInfo == null ? true : (rightInfo.maxBSTSubtreeSize == rightInfo.allSize); + if (leftBST && rightBST) { + boolean leftMaxLessX = leftInfo == null ? true : (leftInfo.max < x.value); + boolean rightMinMoreX = rightInfo == null ? true : (x.value < rightInfo.min); + if (leftMaxLessX && rightMinMoreX) { + int leftSize = leftInfo == null ? 0 : leftInfo.allSize; + int rightSize = rightInfo == null ? 0 : rightInfo.allSize; + p3 = leftSize + rightSize + 1; + } + } + return new Info(Math.max(p1, Math.max(p2, p3)), allSize, max, min); } - - - - // for test public static Node generateRandomBST(int maxLevel, int maxValue) { diff --git a/src/class08_12/Code08_MaxDistance.java b/src/class08_12/Code06_MaxDistance.java similarity index 74% rename from src/class08_12/Code08_MaxDistance.java rename to src/class08_12/Code06_MaxDistance.java index 930c1dc..f624e05 100644 --- a/src/class08_12/Code08_MaxDistance.java +++ b/src/class08_12/Code06_MaxDistance.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -public class Code08_MaxDistance { +public class Code06_MaxDistance { public static class Node { public int value; @@ -92,6 +92,33 @@ public class Code08_MaxDistance { return distance1 + distance2 - 1; } +// public static int maxDistance2(Node head) { +// return process(head).maxDistance; +// } +// +// public static class Info { +// public int maxDistance; +// public int height; +// +// public Info(int dis, int h) { +// maxDistance = dis; +// height = h; +// } +// } +// +// public static Info process(Node X) { +// if (X == null) { +// return new Info(0, 0); +// } +// Info leftInfo = process(X.left); +// Info rightInfo = process(X.right); +// int height = Math.max(leftInfo.height, rightInfo.height) + 1; +// int maxDistance = Math.max( +// Math.max(leftInfo.maxDistance, rightInfo.maxDistance), +// leftInfo.height + rightInfo.height + 1); +// return new Info(maxDistance, height); +// } + public static int maxDistance2(Node head) { return process(head).maxDistance; } @@ -100,22 +127,24 @@ public class Code08_MaxDistance { public int maxDistance; public int height; - public Info(int dis, int h) { - maxDistance = dis; + public Info(int m, int h) { + maxDistance = m; height = h; } + } - public static Info process(Node X) { - if (X == null) { + public static Info process(Node x) { + if (x == null) { return new Info(0, 0); } - Info leftInfo = process(X.left); - Info rightInfo = process(X.right); + Info leftInfo = process(x.left); + Info rightInfo = process(x.right); int height = Math.max(leftInfo.height, rightInfo.height) + 1; - int maxDistance = Math.max( - Math.max(leftInfo.maxDistance, rightInfo.maxDistance), - leftInfo.height + rightInfo.height + 1); + int p1 = leftInfo.maxDistance; + int p2 = rightInfo.maxDistance; + int p3 = leftInfo.height + rightInfo.height + 1; + int maxDistance = Math.max(Math.max(p1, p2), p3); return new Info(maxDistance, height); } diff --git a/src/class08_13/Code01_IsCBT.java b/src/class08_13/Code01_IsCBT.java new file mode 100644 index 0000000..a98c382 --- /dev/null +++ b/src/class08_13/Code01_IsCBT.java @@ -0,0 +1,149 @@ +package class08_13; + +import java.util.LinkedList; + +public class Code01_IsCBT { + + public static class Node { + public int value; + public Node left; + public Node right; + + public Node(int data) { + this.value = data; + } + } + + public static boolean isCBT1(Node head) { + if (head == null) { + return true; + } + LinkedList queue = new LinkedList<>(); + // 是否遇到过左右两个孩子不双全的节点 + boolean leaf = false; + Node l = null; + Node r = null; + queue.add(head); + while (!queue.isEmpty()) { + head = queue.poll(); + l = head.left; + r = head.right; + if ( + // 如果遇到了不双全的节点之后,又发现当前节点不是叶节点 + (leaf && (l != null || r != null)) + || + (l == null && r != null) + + ) { + return false; + } + if (l != null) { + queue.add(l); + } + if (r != null) { + queue.add(r); + } + if (l == null || r == null) { + leaf = true; + } + } + return true; + } + + public static boolean isCBT2(Node head) { + if (head == null) { + return true; + } + return process(head).isCBT; + } + + // 对每一棵子树,是否是满二叉树、是否是完全二叉树、高度 + public static class Info { + public boolean isFull; + public boolean isCBT; + public int height; + + public Info(boolean full, boolean cbt, int h) { + isFull = full; + isCBT = cbt; + height = h; + } + } + + public static Info process(Node X) { + if (X == null) { + return new Info(true, true, 0); + } + Info leftInfo = process(X.left); + Info rightInfo = process(X.right); + + + + int height = Math.max(leftInfo.height, rightInfo.height) + 1; + + + boolean isFull = leftInfo.isFull + && + rightInfo.isFull + && leftInfo.height == rightInfo.height; + + + boolean isCBT = false; + if (isFull) { + isCBT = true; + } else { // 以x为头整棵树,不满 + if (leftInfo.isCBT && rightInfo.isCBT) { + + + if (leftInfo.isCBT + && rightInfo.isFull + && leftInfo.height == rightInfo.height + 1) { + isCBT = true; + } + if (leftInfo.isFull + && + rightInfo.isFull + && leftInfo.height == rightInfo.height + 1) { + isCBT = true; + } + if (leftInfo.isFull + && rightInfo.isCBT && leftInfo.height == rightInfo.height) { + isCBT = true; + } + + + } + } + return new Info(isFull, isCBT, height); + } + + // for test + public static Node generateRandomBST(int maxLevel, int maxValue) { + return generate(1, maxLevel, maxValue); + } + + // for test + public static Node generate(int level, int maxLevel, int maxValue) { + if (level > maxLevel || Math.random() < 0.5) { + return null; + } + Node head = new Node((int) (Math.random() * maxValue)); + head.left = generate(level + 1, maxLevel, maxValue); + head.right = generate(level + 1, maxLevel, maxValue); + return head; + } + + public static void main(String[] args) { + int maxLevel = 5; + int maxValue = 100; + int testTimes = 1000000; + for (int i = 0; i < testTimes; i++) { + Node head = generateRandomBST(maxLevel, maxValue); + if (isCBT1(head) != isCBT2(head)) { + System.out.println("Oops!"); + } + } + System.out.println("finish!"); + } + +} diff --git a/src/class08_12/Code06_MaxSubBSTHead.java b/src/class08_13/Code02_MaxSubBSTHead.java similarity index 98% rename from src/class08_12/Code06_MaxSubBSTHead.java rename to src/class08_13/Code02_MaxSubBSTHead.java index abe7551..b3f5c2b 100644 --- a/src/class08_12/Code06_MaxSubBSTHead.java +++ b/src/class08_13/Code02_MaxSubBSTHead.java @@ -1,8 +1,8 @@ -package class08_12; +package class08_13; import java.util.ArrayList; -public class Code06_MaxSubBSTHead { +public class Code02_MaxSubBSTHead { public static class Node { public int value; diff --git a/src/class08_12/Code07_lowestAncestor.java b/src/class08_13/Code03_lowestAncestor.java similarity index 98% rename from src/class08_12/Code07_lowestAncestor.java rename to src/class08_13/Code03_lowestAncestor.java index a39b301..ddab0a3 100644 --- a/src/class08_12/Code07_lowestAncestor.java +++ b/src/class08_13/Code03_lowestAncestor.java @@ -1,10 +1,10 @@ -package class08_12; +package class08_13; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -public class Code07_lowestAncestor { +public class Code03_lowestAncestor { public static class Node { public int value; diff --git a/src/class08_12/Code09_MaxHappy.java b/src/class08_13/Code04_MaxHappy.java similarity index 98% rename from src/class08_12/Code09_MaxHappy.java rename to src/class08_13/Code04_MaxHappy.java index b52851e..c433d36 100644 --- a/src/class08_12/Code09_MaxHappy.java +++ b/src/class08_13/Code04_MaxHappy.java @@ -1,9 +1,9 @@ -package class08_12; +package class08_13; import java.util.ArrayList; import java.util.List; -public class Code09_MaxHappy { +public class Code04_MaxHappy { public static class Employee { public int happy; diff --git a/src/class09/Code01_LowestLexicography.java b/src/class09_13/Code01_LowestLexicography.java similarity index 99% rename from src/class09/Code01_LowestLexicography.java rename to src/class09_13/Code01_LowestLexicography.java index c5bc3dc..8d2157f 100644 --- a/src/class09/Code01_LowestLexicography.java +++ b/src/class09_13/Code01_LowestLexicography.java @@ -1,4 +1,4 @@ -package class09; +package class09_13; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/class09/Code02_Light.java b/src/class09_14/Code01_Light.java similarity index 94% rename from src/class09/Code02_Light.java rename to src/class09_14/Code01_Light.java index 981419d..05340eb 100644 --- a/src/class09/Code02_Light.java +++ b/src/class09_14/Code01_Light.java @@ -1,8 +1,8 @@ -package class09; +package class09_14; import java.util.HashSet; -public class Code02_Light { +public class Code01_Light { public static int minLight1(String road) { if (road == null || road.length() == 0) { diff --git a/src/class09/Code03_LessMoneySplitGold.java b/src/class09_14/Code02_LessMoneySplitGold.java similarity index 96% rename from src/class09/Code03_LessMoneySplitGold.java rename to src/class09_14/Code02_LessMoneySplitGold.java index 3838941..38c51b6 100644 --- a/src/class09/Code03_LessMoneySplitGold.java +++ b/src/class09_14/Code02_LessMoneySplitGold.java @@ -1,8 +1,8 @@ -package class09; +package class09_14; import java.util.PriorityQueue; -public class Code03_LessMoneySplitGold { +public class Code02_LessMoneySplitGold { public static int lessMoney1(int[] arr) { if (arr == null || arr.length == 0) { diff --git a/src/class09/Code04_BestArrange.java b/src/class09_14/Code03_BestArrange.java similarity index 98% rename from src/class09/Code04_BestArrange.java rename to src/class09_14/Code03_BestArrange.java index 7cf290a..b67be4f 100644 --- a/src/class09/Code04_BestArrange.java +++ b/src/class09_14/Code03_BestArrange.java @@ -1,9 +1,9 @@ -package class09; +package class09_14; import java.util.Arrays; import java.util.Comparator; -public class Code04_BestArrange { +public class Code03_BestArrange { public static class Program { public int start; diff --git a/src/class09/Code05_IPO.java b/src/class09_14/Code04_IPO.java similarity index 96% rename from src/class09/Code05_IPO.java rename to src/class09_14/Code04_IPO.java index bde9d89..0c42e96 100644 --- a/src/class09/Code05_IPO.java +++ b/src/class09_14/Code04_IPO.java @@ -1,9 +1,9 @@ -package class09; +package class09_14; import java.util.Comparator; import java.util.PriorityQueue; -public class Code05_IPO { +public class Code04_IPO { public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) { PriorityQueue minCostQ = new PriorityQueue<>(new MinCostComparator());