modify code

master
algorithmzuo 3 years ago
parent 311a996d85
commit 163da249f0

@ -1,154 +1,24 @@
package class12;
import java.util.ArrayList;
// 在线测试链接 : https://leetcode.com/problems/largest-bst-subtree
public class Code05_MaxSubBSTSize {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static int getBSTSize(Node head) {
if (head == null) {
return 0;
}
ArrayList<Node> arr = new ArrayList<>();
in(head, arr);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i).value <= arr.get(i - 1).value) {
return 0;
}
}
return arr.size();
}
// 提交时不要提交这个类
public static class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public static void in(Node head, ArrayList<Node> arr) {
if (head == null) {
return;
public TreeNode(int value) {
val = value;
}
in(head.left, arr);
arr.add(head);
in(head.right, arr);
}
public static int maxSubBSTSize1(Node head) {
// 提交如下的代码,可以直接通过
public static int largestBSTSubtree(TreeNode head) {
if (head == null) {
return 0;
}
int h = getBSTSize(head);
if (h != 0) {
return h;
}
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 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(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;
//
//
// }
// return new Info(isAllBST, maxSubBSTSize, min, max);
// }
public static int maxSubBSTSize2(Node head) {
if(head == null) {
return 0;
}
return process(head).maxBSTSubtreeSize;
}
@ -166,14 +36,14 @@ public class Code05_MaxSubBSTSize {
}
}
public static Info process(Node x) {
public static Info process(TreeNode x) {
if (x == null) {
return null;
}
Info leftInfo = process(x.left);
Info rightInfo = process(x.right);
int max = x.value;
int min = x.value;
int max = x.val;
int min = x.val;
int allSize = 1;
if (leftInfo != null) {
max = Math.max(leftInfo.max, max);
@ -197,8 +67,8 @@ public class Code05_MaxSubBSTSize {
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);
boolean leftMaxLessX = leftInfo == null ? true : (leftInfo.max < x.val);
boolean rightMinMoreX = rightInfo == null ? true : (x.val < rightInfo.min);
if (leftMaxLessX && rightMinMoreX) {
int leftSize = leftInfo == null ? 0 : leftInfo.allSize;
int rightSize = rightInfo == null ? 0 : rightInfo.allSize;
@ -208,33 +78,4 @@ public class Code05_MaxSubBSTSize {
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);
}
// 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 = 4;
int maxValue = 100;
int testTimes = 1000000;
for (int i = 0; i < testTimes; i++) {
Node head = generateRandomBST(maxLevel, maxValue);
if (maxSubBSTSize1(head) != maxSubBSTSize2(head)) {
System.out.println("Oops!");
}
}
System.out.println("finish!");
}
}

Loading…
Cancel
Save