modify code

master
algorithmzuo 2 years ago
parent db93f1d05d
commit 8c62879d53

@ -0,0 +1,54 @@
package 03.mca_03;
import java.util.Comparator;
import java.util.PriorityQueue;
// 测试链接https://leetcode.cn/problems/merge-k-sorted-lists/
public class Code01_MergeKSortedLists {
// 这个类不提交
public static class ListNode {
public int val;
public ListNode next;
}
// 提交以下方法
public static class ListNodeComparator implements Comparator<ListNode> {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
}
public static ListNode mergeKLists(ListNode[] lists) {
if (lists == null) {
return null;
}
PriorityQueue<ListNode> heap = new PriorityQueue<>(new ListNodeComparator());
for (int i = 0; i < lists.length; i++) {
if (lists[i] != null) {
heap.add(lists[i]);
}
}
if (heap.isEmpty()) {
return null;
}
ListNode head = heap.poll();
ListNode pre = head;
if (pre.next != null) {
heap.add(pre.next);
}
while (!heap.isEmpty()) {
ListNode cur = heap.poll();
pre.next = cur;
pre = cur;
if (cur.next != null) {
heap.add(cur.next);
}
}
return head;
}
}

@ -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,43 @@
package 03.mca_03;
import java.util.HashMap;
//测试链接https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal
public class Code03_ConstructBinaryTreeFromPreorderAndInorderTraversal {
// 提交时不提交这个类
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
// 提交如下的方法
public static TreeNode buildTree(int[] pre, int[] in) {
HashMap<Integer, Integer> valueIndexMap = new HashMap<>();
for (int i = 0; i < in.length; i++) {
valueIndexMap.put(in[i], i);
}
return g(pre, 0, pre.length - 1, in, 0, in.length - 1, valueIndexMap);
}
public static TreeNode g(int[] pre, int L1, int R1, int[] in, int L2, int R2,
HashMap<Integer, Integer> valueIndexMap) {
if (L1 > R1) {
return null;
}
TreeNode head = new TreeNode(pre[L1]);
if (L1 == R1) {
return head;
}
int find = valueIndexMap.get(pre[L1]);
head.left = g(pre, L1 + 1, L1 + find - L2, in, L2, find - 1, valueIndexMap);
head.right = g(pre, L1 + find - L2 + 1, R1, in, find + 1, R2, valueIndexMap);
return head;
}
}

@ -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,45 @@
package 03.mca_03;
//本题测试链接 : https://leetcode.cn/problems/count-complete-tree-nodes/
public class Code07_CompleteTreeNodeNumber {
// 提交时不要提交这个类
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
}
// 提交如下的方法
public static int countNodes(TreeNode head) {
if (head == null) {
return 0;
}
return bs(head, 1, mostLeftLevel(head, 1));
}
// 当前来到node节点node节点在level层总层数是h
// 返回node为头的子树(必是完全二叉树),有多少个节点
public static int bs(TreeNode node, int Level, int h) {
if (Level == h) {
return 1;
}
if (mostLeftLevel(node.right, Level + 1) == h) {
return (1 << (h - Level)) + bs(node.right, Level + 1, h);
} else {
return (1 << (h - Level - 1)) + bs(node.left, Level + 1, h);
}
}
// 如果node在第level层
// 求以node为头的子树最大深度是多少
// node为头的子树一定是完全二叉树
public static int mostLeftLevel(TreeNode node, int level) {
while (node != null) {
level++;
node = node.left;
}
return level - 1;
}
}

@ -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…
Cancel
Save