From 8c62879d5301ca420e00c3830bfe31737162b7cb Mon Sep 17 00:00:00 2001 From: algorithmzuo Date: Thu, 23 Feb 2023 15:52:14 +0800 Subject: [PATCH] modify code --- .../mca_03/Code01_MergeKSortedLists.java | 54 +++++++++++++ .../mca_03/Code02_LevelTraversalBT.java | 47 +++++++++++ ...ryTreeFromPreorderAndInorderTraversal.java | 43 ++++++++++ .../第03期/mca_03/Code04_IsCBT.java | 49 +++++++++++ .../第03期/mca_03/Code05_IsBST.java | 63 +++++++++++++++ .../第03期/mca_03/Code06_IsBalanced.java | 44 ++++++++++ .../mca_03/Code07_CompleteTreeNodeNumber.java | 45 +++++++++++ .../第03期/mca_03/Code08_MaxSubBSTSize.java | 81 +++++++++++++++++++ .../mca_03/Code09_DiameterOfBinaryTree.java | 45 +++++++++++ 9 files changed, 471 insertions(+) create mode 100644 MCA算法突击课/第03期/mca_03/Code01_MergeKSortedLists.java create mode 100644 MCA算法突击课/第03期/mca_03/Code02_LevelTraversalBT.java create mode 100644 MCA算法突击课/第03期/mca_03/Code03_ConstructBinaryTreeFromPreorderAndInorderTraversal.java create mode 100644 MCA算法突击课/第03期/mca_03/Code04_IsCBT.java create mode 100644 MCA算法突击课/第03期/mca_03/Code05_IsBST.java create mode 100644 MCA算法突击课/第03期/mca_03/Code06_IsBalanced.java create mode 100644 MCA算法突击课/第03期/mca_03/Code07_CompleteTreeNodeNumber.java create mode 100644 MCA算法突击课/第03期/mca_03/Code08_MaxSubBSTSize.java create mode 100644 MCA算法突击课/第03期/mca_03/Code09_DiameterOfBinaryTree.java diff --git a/MCA算法突击课/第03期/mca_03/Code01_MergeKSortedLists.java b/MCA算法突击课/第03期/mca_03/Code01_MergeKSortedLists.java new file mode 100644 index 0000000..e1e137a --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code01_MergeKSortedLists.java @@ -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 { + + @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 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; + } + +} diff --git a/MCA算法突击课/第03期/mca_03/Code02_LevelTraversalBT.java b/MCA算法突击课/第03期/mca_03/Code02_LevelTraversalBT.java new file mode 100644 index 0000000..f952851 --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code02_LevelTraversalBT.java @@ -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> levelOrder(TreeNode root) { + List> ans = new ArrayList<>(); + if (root != null) { + Queue queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + int size = queue.size(); + List 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; + } + +} diff --git a/MCA算法突击课/第03期/mca_03/Code03_ConstructBinaryTreeFromPreorderAndInorderTraversal.java b/MCA算法突击课/第03期/mca_03/Code03_ConstructBinaryTreeFromPreorderAndInorderTraversal.java new file mode 100644 index 0000000..3723f7b --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code03_ConstructBinaryTreeFromPreorderAndInorderTraversal.java @@ -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 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 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; + } + +} diff --git a/MCA算法突击课/第03期/mca_03/Code04_IsCBT.java b/MCA算法突击课/第03期/mca_03/Code04_IsCBT.java new file mode 100644 index 0000000..cedc300 --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code04_IsCBT.java @@ -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 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; + } + +} diff --git a/MCA算法突击课/第03期/mca_03/Code05_IsBST.java b/MCA算法突击课/第03期/mca_03/Code05_IsBST.java new file mode 100644 index 0000000..0574bc6 --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code05_IsBST.java @@ -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); + } + +} diff --git a/MCA算法突击课/第03期/mca_03/Code06_IsBalanced.java b/MCA算法突击课/第03期/mca_03/Code06_IsBalanced.java new file mode 100644 index 0000000..cb5dba6 --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code06_IsBalanced.java @@ -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); + } + +} diff --git a/MCA算法突击课/第03期/mca_03/Code07_CompleteTreeNodeNumber.java b/MCA算法突击课/第03期/mca_03/Code07_CompleteTreeNodeNumber.java new file mode 100644 index 0000000..2009756 --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code07_CompleteTreeNodeNumber.java @@ -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; + } + +} diff --git a/MCA算法突击课/第03期/mca_03/Code08_MaxSubBSTSize.java b/MCA算法突击课/第03期/mca_03/Code08_MaxSubBSTSize.java new file mode 100644 index 0000000..5f891ec --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code08_MaxSubBSTSize.java @@ -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); + } + +} diff --git a/MCA算法突击课/第03期/mca_03/Code09_DiameterOfBinaryTree.java b/MCA算法突击课/第03期/mca_03/Code09_DiameterOfBinaryTree.java new file mode 100644 index 0000000..1ccc338 --- /dev/null +++ b/MCA算法突击课/第03期/mca_03/Code09_DiameterOfBinaryTree.java @@ -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); + } + +}