parent
db93f1d05d
commit
8c62879d53
@ -0,0 +1,47 @@
|
||||
package 第03期.mca_03;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
// 测试链接 : https://leetcode.cn/problems/binary-tree-level-order-traversal/
|
||||
public class Code02_LevelTraversalBT {
|
||||
|
||||
// 不提交这个类
|
||||
public static class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int v) {
|
||||
val = v;
|
||||
}
|
||||
}
|
||||
|
||||
// 提交以下的方法
|
||||
public static List<List<Integer>> levelOrder(TreeNode root) {
|
||||
List<List<Integer>> ans = new ArrayList<>();
|
||||
if (root != null) {
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
List<Integer> curLevel = new ArrayList<>();
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode cur = queue.poll();
|
||||
if (cur.left != null) {
|
||||
queue.add(cur.left);
|
||||
}
|
||||
if (cur.right != null) {
|
||||
queue.add(cur.right);
|
||||
}
|
||||
curLevel.add(cur.val);
|
||||
}
|
||||
ans.add(curLevel);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package 第03期.mca_03;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
// 测试链接 : https://leetcode.cn/problems/check-completeness-of-a-binary-tree/
|
||||
public class Code04_IsCBT {
|
||||
|
||||
// 提交时不提交这个类
|
||||
public static class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int v) {
|
||||
val = v;
|
||||
}
|
||||
}
|
||||
|
||||
// 提交以下的方法
|
||||
public static boolean isCompleteTree(TreeNode head) {
|
||||
if (head == null) {
|
||||
return true;
|
||||
}
|
||||
LinkedList<TreeNode> queue = new LinkedList<>();
|
||||
boolean leaf = false;
|
||||
TreeNode l = null;
|
||||
TreeNode 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;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package 第03期.mca_03;
|
||||
|
||||
// 测试链接 : https://leetcode.cn/problems/validate-binary-search-tree/
|
||||
public class Code05_IsBST {
|
||||
|
||||
// 提交时不提交这个类
|
||||
public static class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int v) {
|
||||
val = v;
|
||||
}
|
||||
}
|
||||
|
||||
// 提交以下的方法
|
||||
public boolean isValidBST(TreeNode root) {
|
||||
if (root == null) {
|
||||
return true;
|
||||
}
|
||||
return process(root).isBST;
|
||||
}
|
||||
|
||||
public static class Info {
|
||||
public boolean isBST;
|
||||
public int min;
|
||||
public int max;
|
||||
|
||||
public Info(boolean a, int b, int c) {
|
||||
isBST = a;
|
||||
min = b;
|
||||
max = c;
|
||||
}
|
||||
}
|
||||
|
||||
public static Info process(TreeNode root) {
|
||||
if (root == null) {
|
||||
return null;
|
||||
}
|
||||
Info left = process(root.left);
|
||||
Info right = process(root.right);
|
||||
int min = root.val;
|
||||
int max = root.val;
|
||||
boolean isBST = true;
|
||||
if (left != null) {
|
||||
min = Math.min(min, left.min);
|
||||
max = Math.max(max, left.max);
|
||||
if (!left.isBST || left.max >= root.val) {
|
||||
isBST = false;
|
||||
}
|
||||
}
|
||||
if (right != null) {
|
||||
min = Math.min(min, right.min);
|
||||
max = Math.max(max, right.max);
|
||||
if (!right.isBST || root.val >= right.min) {
|
||||
isBST = false;
|
||||
}
|
||||
}
|
||||
return new Info(isBST, min, max);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package 第03期.mca_03;
|
||||
|
||||
// 测试链接 : https://leetcode.cn/problems/balanced-binary-tree/
|
||||
public class Code06_IsBalanced {
|
||||
|
||||
// 提交时不提交这个类
|
||||
public static class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int v) {
|
||||
val = v;
|
||||
}
|
||||
}
|
||||
|
||||
// 提交以下的方法
|
||||
public static boolean isBalanced(TreeNode root) {
|
||||
return process(root).isBalanced;
|
||||
}
|
||||
|
||||
public static class Info {
|
||||
public boolean isBalanced;
|
||||
public int height;
|
||||
|
||||
public Info(boolean i, int h) {
|
||||
isBalanced = i;
|
||||
height = h;
|
||||
}
|
||||
}
|
||||
|
||||
public static Info process(TreeNode root) {
|
||||
if (root == null) {
|
||||
return new Info(true, 0);
|
||||
}
|
||||
Info leftInfo = process(root.left);
|
||||
Info rightInfo = process(root.right);
|
||||
int height = Math.max(leftInfo.height, rightInfo.height) + 1;
|
||||
boolean isBalanced = leftInfo.isBalanced && rightInfo.isBalanced
|
||||
&& Math.abs(leftInfo.height - rightInfo.height) < 2;
|
||||
return new Info(isBalanced, height);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package 第03期.mca_03;
|
||||
|
||||
// 测试链接 : https://leetcode.com/problems/largest-bst-subtree
|
||||
public class Code08_MaxSubBSTSize {
|
||||
|
||||
// 提交时不要提交这个类
|
||||
public static class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int value) {
|
||||
val = value;
|
||||
}
|
||||
}
|
||||
|
||||
// 提交如下方法
|
||||
public static int largestBSTSubtree(TreeNode head) {
|
||||
if (head == null) {
|
||||
return 0;
|
||||
}
|
||||
return process(head).maxBSTSubtreeSize;
|
||||
}
|
||||
|
||||
public static class Info {
|
||||
public int maxBSTSubtreeSize;
|
||||
public int allSize;
|
||||
public int max;
|
||||
public int min;
|
||||
|
||||
public Info(int m, int a, int ma, int mi) {
|
||||
maxBSTSubtreeSize = m;
|
||||
allSize = a;
|
||||
max = ma;
|
||||
min = mi;
|
||||
}
|
||||
}
|
||||
|
||||
public static Info process(TreeNode x) {
|
||||
if (x == null) {
|
||||
return null;
|
||||
}
|
||||
Info leftInfo = process(x.left);
|
||||
Info rightInfo = process(x.right);
|
||||
int max = x.val;
|
||||
int min = x.val;
|
||||
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.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;
|
||||
p3 = leftSize + rightSize + 1;
|
||||
}
|
||||
}
|
||||
return new Info(Math.max(p1, Math.max(p2, p3)), allSize, max, min);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package 第03期.mca_03;
|
||||
|
||||
// 测试链接 : https://leetcode.cn/problems/diameter-of-binary-tree/
|
||||
public class Code09_DiameterOfBinaryTree {
|
||||
|
||||
// 提交时不要提交这个类
|
||||
public static class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
|
||||
public TreeNode(int value) {
|
||||
val = value;
|
||||
}
|
||||
}
|
||||
|
||||
// 提交以下的方法
|
||||
public static int diameterOfBinaryTree(TreeNode root) {
|
||||
return process(root).maxDistance;
|
||||
}
|
||||
|
||||
public static class Info {
|
||||
public int maxDistance;
|
||||
public int height;
|
||||
|
||||
public Info(int m, int h) {
|
||||
maxDistance = m;
|
||||
height = h;
|
||||
}
|
||||
}
|
||||
|
||||
public static Info process(TreeNode 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 p1 = Math.max(leftInfo.maxDistance, rightInfo.maxDistance);
|
||||
int p2 = leftInfo.height + rightInfo.height;
|
||||
int maxDistance = Math.max(p1, p2);
|
||||
return new Info(maxDistance, height);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue