modify code structure

pull/6/head
左程云 5 years ago
parent 6a78267ec5
commit 976a57dac7

@ -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;

@ -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

@ -32,34 +32,44 @@ 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) {
return generate(1, maxLevel, maxValue);

@ -48,155 +48,166 @@ 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
// (leftInfo == null ? true : leftInfo.max < X.value)
// &&
// (rightInfo == null ? true : rightInfo.min > 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
(leftInfo == null ? true : leftInfo.max < X.value)
&&
(rightInfo == null ? true : rightInfo.min > X.value)
) {
maxSubBSTSize =
(leftInfo == null ? 0 : leftInfo.maxSubBSTSize)
+
(rightInfo == null ? 0 : rightInfo.maxSubBSTSize)
+
1;
isAllBST = true;
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(isAllBST, maxSubBSTSize, min, max);
return new Info(Math.max(p1, Math.max(p2, p3)), allSize, max, min);
}
// for test
public static Node generateRandomBST(int maxLevel, int maxValue) {
return generate(1, maxLevel, maxValue);

@ -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);
}

@ -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<Node> 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!");
}
}

@ -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;

@ -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;

@ -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;

@ -1,4 +1,4 @@
package class09;
package class09_13;
import java.util.ArrayList;
import java.util.Arrays;

@ -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) {

@ -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) {

@ -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;

@ -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<Program> minCostQ = new PriorityQueue<>(new MinCostComparator());
Loading…
Cancel
Save