modify code structure

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

@ -30,7 +30,9 @@ public class Code01_IsCBT {
r = head.right; r = head.right;
if ( if (
// 如果遇到了不双全的节点之后,又发现当前节点不是叶节点 // 如果遇到了不双全的节点之后,又发现当前节点不是叶节点
(leaf && (l != null || r != null)) || (l == null && r != null) (leaf && (l != null || r != null))
||
(l == null && r != null)
) { ) {
return false; return false;

@ -45,42 +45,52 @@ public class Code02_IsBST {
} }
public static class Info { public static class Info {
boolean isBST; public boolean isBST;
public int min;
public int max; public int max;
public int min;
public Info(boolean is, int mi, int ma) { public Info(boolean i, int ma, int mi) {
isBST = is; isBST = i;
min = mi;
max = ma; max = ma;
min = mi;
} }
} }
public static Info process(Node head) { public static Info process(Node x) {
if (head == null) { if (x == null) {
return null; return null;
} }
Info leftInfo = process(head.left); Info leftInfo = process(x.left);
Info rightInfo = process(head.right); Info rightInfo = process(x.right);
int min = head.value; int max = x.value;
int max = head.value;
if (leftInfo != null) { if (leftInfo != null) {
min = Math.min(min, leftInfo.min);
max = Math.max(max, leftInfo.max); max = Math.max(max, leftInfo.max);
} }
if (rightInfo != null) { if (rightInfo != null) {
min = Math.min(min, rightInfo.min);
max = Math.max(max, rightInfo.max); max = Math.max(max, rightInfo.max);
} }
boolean isBST = false; int min = x.value;
if ( if (leftInfo != null) {
(leftInfo == null ? true : (leftInfo.isBST && leftInfo.max < head.value)) min = Math.min(min, leftInfo.min);
&& }
(rightInfo == null ? true : (rightInfo.isBST && rightInfo.min > head.value)) if (rightInfo != null) {
) { min = Math.min(min, rightInfo.min);
isBST = true; }
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 // for test

@ -32,34 +32,44 @@ public class Code03_IsBalanced {
} }
public static boolean isBalanced2(Node head) { public static boolean isBalanced2(Node head) {
return process2(head).isBalaced; return process(head).isBalanced;
} }
// 左、右要求一样Info 信息返回的结构体 public static class Info{
public static class Info { public boolean isBalanced;
public boolean isBalaced;
public int height; public int height;
public Info(boolean b, int h) { public Info(boolean i, int h) {
isBalaced = b; isBalanced = i;
height = h; height = h;
} }
} }
public static Info process2(Node X) { public static Info process(Node x) {
if (X == null) { if(x == null) {
return new Info(true, 0); return new Info(true, 0);
} }
Info leftInfo = process2(X.left); Info leftInfo = process(x.left);
Info rightInfo = process2(X.right); Info rightInfo = process(x.right);
int height = Math.max(leftInfo.height, rightInfo.height) + 1; int height = Math.max(leftInfo.height, rightInfo.height) + 1;
boolean isBalanced = true; 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; isBalanced = false;
} }
return new Info(isBalanced, height); return new Info(isBalanced, height);
} }
// for test // for test
public static Node generateRandomBST(int maxLevel, int maxValue) { public static Node generateRandomBST(int maxLevel, int maxValue) {
return generate(1, maxLevel, maxValue); return generate(1, maxLevel, maxValue);

@ -48,155 +48,166 @@ public class Code05_MaxSubBSTSize {
return Math.max(maxSubBSTSize1(head.left), maxSubBSTSize1(head.right)); return Math.max(maxSubBSTSize1(head.left), maxSubBSTSize1(head.right));
} }
public static int maxSubBSTSize2(Node head) { // public static int maxSubBSTSize2(Node head) {
if (head == null) {
return 0;
}
return process(head).maxSubBSTSize;
}
// public static Info process(Node head) {
// if (head == null) { // 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; // return null;
// } // }
// Info leftInfo = process(head.left); // Info leftInfo = process(X.left);
// Info rightInfo = process(head.right); // Info rightInfo = process(X.right);
// int min = head.value; //
// int max = head.value; //
// int maxSubBSTSize = 0; //
// if (leftInfo != null) { // int min = X.value;
// int max = X.value;
//
// if(leftInfo != null) {
// min = Math.min(min, leftInfo.min); // min = Math.min(min, leftInfo.min);
// max = Math.max(max, leftInfo.max); // max = Math.max(max, leftInfo.max);
// maxSubBSTSize = Math.max(maxSubBSTSize, leftInfo.maxSubBSTSize);
// } // }
// if (rightInfo != null) { // if(rightInfo != null) {
// min = Math.min(min, rightInfo.min); // min = Math.min(min, rightInfo.min);
// max = Math.max(max, rightInfo.max); // max = Math.max(max, rightInfo.max);
// }
//
//
//
//
//
//
//
// int maxSubBSTSize = 0;
// if(leftInfo != null) {
// maxSubBSTSize = leftInfo.maxSubBSTSize;
// }
// if(rightInfo !=null) {
// maxSubBSTSize = Math.max(maxSubBSTSize, rightInfo.maxSubBSTSize); // maxSubBSTSize = Math.max(maxSubBSTSize, rightInfo.maxSubBSTSize);
// } // }
// boolean isBST = false; // boolean isAllBST = false;
// if ((leftInfo == null ? true : (leftInfo.isAllBST && leftInfo.max < head.value)) //
// && (rightInfo == null ? true : (rightInfo.isAllBST && rightInfo.min > head.value))) { //
// isBST = true; // if(
// maxSubBSTSize = (leftInfo == null ? 0 : leftInfo.maxSubBSTSize) // // 左树整体需要是搜索二叉树
// + (rightInfo == null ? 0 : rightInfo.maxSubBSTSize) + 1; // ( 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 static class Info {
public boolean isAllBST; public int maxBSTSubtreeSize;
public int maxSubBSTSize; public int allSize;
public int min;
public int max; public int max;
public int min;
public Info(boolean is, int size, int mi, int ma) { public Info(int m, int a, int ma, int mi) {
isAllBST = is; maxBSTSubtreeSize = m;
maxSubBSTSize = size; allSize = a;
min = mi;
max = ma; max = ma;
min = mi;
} }
} }
public static Info process(Node x) {
if (x == null) {
public static Info process(Node X) {
if(X == null) {
return null; return null;
} }
Info leftInfo = process(X.left); Info leftInfo = process(x.left);
Info rightInfo = process(X.right); Info rightInfo = process(x.right);
int max = x.value;
int min = x.value;
int allSize = 1;
int min = X.value; if (leftInfo != null) {
int max = X.value; max = Math.max(leftInfo.max, max);
min = Math.min(leftInfo.min, min);
if(leftInfo != null) { allSize += leftInfo.allSize;
min = Math.min(min, leftInfo.min); }
max = Math.max(max, leftInfo.max); if (rightInfo != null) {
} max = Math.max(rightInfo.max, max);
if(rightInfo != null) { min = Math.min(rightInfo.min, min);
min = Math.min(min, rightInfo.min); allSize += rightInfo.allSize;
max = Math.max(max, rightInfo.max); }
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);
} }
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;
}
return new Info(isAllBST, maxSubBSTSize, min, max);
}
// for test // for test
public static Node generateRandomBST(int maxLevel, int maxValue) { public static Node generateRandomBST(int maxLevel, int maxValue) {
return generate(1, maxLevel, maxValue); return generate(1, maxLevel, maxValue);

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
public class Code08_MaxDistance { public class Code06_MaxDistance {
public static class Node { public static class Node {
public int value; public int value;
@ -92,6 +92,33 @@ public class Code08_MaxDistance {
return distance1 + distance2 - 1; 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) { public static int maxDistance2(Node head) {
return process(head).maxDistance; return process(head).maxDistance;
} }
@ -100,22 +127,24 @@ public class Code08_MaxDistance {
public int maxDistance; public int maxDistance;
public int height; public int height;
public Info(int dis, int h) { public Info(int m, int h) {
maxDistance = dis; maxDistance = m;
height = h; height = h;
} }
} }
public static Info process(Node X) { public static Info process(Node x) {
if (X == null) { if (x == null) {
return new Info(0, 0); return new Info(0, 0);
} }
Info leftInfo = process(X.left); Info leftInfo = process(x.left);
Info rightInfo = process(X.right); Info rightInfo = process(x.right);
int height = Math.max(leftInfo.height, rightInfo.height) + 1; int height = Math.max(leftInfo.height, rightInfo.height) + 1;
int maxDistance = Math.max( int p1 = leftInfo.maxDistance;
Math.max(leftInfo.maxDistance, rightInfo.maxDistance), int p2 = rightInfo.maxDistance;
leftInfo.height + rightInfo.height + 1); int p3 = leftInfo.height + rightInfo.height + 1;
int maxDistance = Math.max(Math.max(p1, p2), p3);
return new Info(maxDistance, height); 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; import java.util.ArrayList;
public class Code06_MaxSubBSTHead { public class Code02_MaxSubBSTHead {
public static class Node { public static class Node {
public int value; public int value;

@ -1,10 +1,10 @@
package class08_12; package class08_13;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
public class Code07_lowestAncestor { public class Code03_lowestAncestor {
public static class Node { public static class Node {
public int value; public int value;

@ -1,9 +1,9 @@
package class08_12; package class08_13;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Code09_MaxHappy { public class Code04_MaxHappy {
public static class Employee { public static class Employee {
public int happy; public int happy;

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

@ -1,8 +1,8 @@
package class09; package class09_14;
import java.util.HashSet; import java.util.HashSet;
public class Code02_Light { public class Code01_Light {
public static int minLight1(String road) { public static int minLight1(String road) {
if (road == null || road.length() == 0) { if (road == null || road.length() == 0) {

@ -1,8 +1,8 @@
package class09; package class09_14;
import java.util.PriorityQueue; import java.util.PriorityQueue;
public class Code03_LessMoneySplitGold { public class Code02_LessMoneySplitGold {
public static int lessMoney1(int[] arr) { public static int lessMoney1(int[] arr) {
if (arr == null || arr.length == 0) { if (arr == null || arr.length == 0) {

@ -1,9 +1,9 @@
package class09; package class09_14;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
public class Code04_BestArrange { public class Code03_BestArrange {
public static class Program { public static class Program {
public int start; public int start;

@ -1,9 +1,9 @@
package class09; package class09_14;
import java.util.Comparator; import java.util.Comparator;
import java.util.PriorityQueue; import java.util.PriorityQueue;
public class Code05_IPO { public class Code04_IPO {
public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) { public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) {
PriorityQueue<Program> minCostQ = new PriorityQueue<>(new MinCostComparator()); PriorityQueue<Program> minCostQ = new PriorityQueue<>(new MinCostComparator());
Loading…
Cancel
Save