From 1044629a5eaf878e429c1a966fb470cd221913d5 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 31 Mar 2024 15:58:42 +0800 Subject: [PATCH] redo --- .../cn/whaifree/leetCode/model/TreeNode.java | 8 +- .../cn/whaifree/redo/redoAll/2024_3_30.md | 7 ++ .../cn/whaifree/redo/redoAll/LeetCode101.java | 78 +++++++++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode105.java | 55 +++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode110.java | 42 ++++++++++ .../cn/whaifree/redo/redoAll/LeetCode199.java | 68 ++++++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode216.java | 57 ++++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode222.java | 58 ++++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode246.java | 54 +++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode257.java | 59 ++++++++++++++ .../cn/whaifree/redo/redoAll/LeetCode77.java | 57 ++++++++++++++ 11 files changed, 542 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode101.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode105.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode110.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode199.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode216.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode222.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode246.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode257.java create mode 100644 src/main/java/cn/whaifree/redo/redoAll/LeetCode77.java diff --git a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java index 10e79c1..06347bd 100644 --- a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java +++ b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java @@ -1,7 +1,6 @@ package cn.whaifree.leetCode.model; import java.util.*; -import java.util.function.Consumer; /** * @version 1.0 @@ -43,6 +42,13 @@ public class TreeNode { // } // } + + @Override + public String toString() { + printTree(this); + return null; + } + public static List treeToArray(TreeNode root) { List result = new ArrayList<>(); if (root == null) diff --git a/src/main/java/cn/whaifree/redo/redoAll/2024_3_30.md b/src/main/java/cn/whaifree/redo/redoAll/2024_3_30.md index 4b8930e..5dcb1ad 100644 --- a/src/main/java/cn/whaifree/redo/redoAll/2024_3_30.md +++ b/src/main/java/cn/whaifree/redo/redoAll/2024_3_30.md @@ -5,5 +5,12 @@ [LeetCode15.java](LeetCode15.java) 水平遍历 [LeetCode94_145_144_102.java](LeetCode94_145_144_102.java) +[LeetCode199.java](LeetCode199.java) + +[LeetCode105.java](LeetCode105.java) 构造二叉树 + + + + diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode101.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode101.java new file mode 100644 index 0000000..51180cf --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode101.java @@ -0,0 +1,78 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/31 11:53 + * @注释 + */ +public class LeetCode101 { + @Test + public void test() + { + System.out.println(new Solution1().isSymmetric(TreeNode.constructTreeByArray(1,2,3))); + } + + class Solution { + public boolean isSymmetric(TreeNode root) { + return isSyn(root.left, root.right); + } + + public boolean isSyn(TreeNode one, TreeNode two) { + if (one == null && two == null) { + return true; + } else if (one == null || two == null) { + return false; + } else if (one.val != two.val) { + return false; + } + + boolean a = isSyn(one.left, two.right); + boolean b = isSyn(one.right, two.left); + + return a && b; + } + } + + class Solution1 { + public boolean isSymmetric(TreeNode root) { + if (root.left == null && root.right == null) { + return true; + }else if (root.left == null || root.right == null) { + return false; + } + + Deque deque = new LinkedList<>(); + deque.add(root.left); + deque.add(root.right); + while (!deque.isEmpty()) { + TreeNode one = deque.pop(); + TreeNode two = deque.pop(); + if (one != null && two == null) { + return false; + } else if (one == null && two != null) { + return false; + } else if (one == null && two == null) { + continue; + } + if (one.val != two.val) { + return false; + } + deque.add(one.left); + deque.add(two.right); + deque.add(one.right); + deque.add(two.left); + } + + return true; + } + + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode105.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode105.java new file mode 100644 index 0000000..d3960d5 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode105.java @@ -0,0 +1,55 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/31 13:32 + * @注释 + */ +public class LeetCode105 { + @Test + public void test() { + Solution solution = new Solution(); + int[] preorder = {3, 9, 20, 15, 7}; + int[] inorder = {9, 3, 15, 20, 7}; + TreeNode treeNode = solution.buildTree(preorder, inorder); + treeNode.printTree(); + } + + class Solution { + public TreeNode buildTree(int[] preorder, int[] inorder) { + return build(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1); + } + + /** + * preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] + */ + public TreeNode build( + int[] preorder, + int[] inorder, + int preStart, + int preEnd, + int inStart, + int inEnd + ) { + if (preStart > preEnd || inStart > inEnd) { + return null; + } + int head = preorder[preStart]; + TreeNode h = new TreeNode(head); + + int inS = 0; // 标记中序中在哪 + while (inorder[inS] != head) { + inS++; + } + int l = inS - inStart; // 左边有几个元素 + h.left = build(preorder, inorder, preStart + 1, preStart + l, inStart, inS - 1); + h.right = build(preorder, inorder, preStart + l + 1, preEnd, inS + 1, inEnd); + return h; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode110.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode110.java new file mode 100644 index 0000000..db84f05 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode110.java @@ -0,0 +1,42 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/31 12:29 + * @注释 + */ +public class LeetCode110{ + + @Test + public void test() + { + Solution solution = new Solution(); + System.out.println(solution.isBalanced(TreeNode.constructTreeByArray(1,2,2,3,null,null,3,4,null,null,4))); + } + + + class Solution { + public boolean isBalanced(TreeNode root) { + return getLevel(root) != -1; + } + + public int getLevel(TreeNode root) { + if (root == null) { + return 0; + } + int left = getLevel(root.left); + int right = getLevel(root.right); + if (left == -1 || right == -1) { + return -1; + } + if (Math.abs(left - right) > 1) { + return -1; + } + return Math.max(left, right) + 1; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode199.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode199.java new file mode 100644 index 0000000..665cd77 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode199.java @@ -0,0 +1,68 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/31 11:37 + * @注释 + */ +public class LeetCode199 { + @Test + public void test() { + + Solution1 solution = new Solution1(); + List integers = solution.rightSideView(TreeNode.constructTreeByArray(1, 2, 3, null, 5, null, null)); + for (int i = 0; i < integers.size(); i++) { + System.out.println(integers.get(i)); + } + } + + class Solution { + public List rightSideView(TreeNode root) { + List res = new ArrayList<>(); + if (root == null) { + return res; + } + // 层次遍历,每层的最后一个 + Deque queue = new ArrayDeque<>(); + queue.add(root); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size ; i++) { + TreeNode e = queue.pop(); + if (e.left != null) queue.add(e.left); + if (e.right != null) queue.add(e.right); + if (i == size - 1) { + res.add(e.val); + } + } + + } + return res; + } + } + class Solution1 { + List res = new ArrayList<>(); + public List rightSideView(TreeNode root) { + level(root, 0); + return res; + } + + public void level(TreeNode root, int level) { + if (root == null) { + return; + } + if (res.size() == level) res.add(root.val); + level(root.right, level + 1); + level(root.left, level + 1); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode216.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode216.java new file mode 100644 index 0000000..8801d7d --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode216.java @@ -0,0 +1,57 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/31 15:51 + * @注释 + */ +public class LeetCode216 +{ + + + @Test + public void test() + { + Solution solution = new Solution(); + List> lists = solution.combinationSum3(2, 18); + System.out.println(lists); + } + + + class Solution { + + List> res; + List path; + int sum = 0; + public List> combinationSum3(int k, int n) { + res = new ArrayList<>(); + path = new ArrayList<>(); + backTracking(k, n, 1); + return res; + } + + public void backTracking(int k, int n, int startIndex) + { + if (k == path.size() && n == sum) { + res.add(new ArrayList<>(path)); + } + + for (int i = startIndex; i < 10; i++) { + if (sum + i > n) { + break; + } + sum += i; + path.add(i); + backTracking(k, n, i + 1); + sum -= i; + path.remove(path.size() - 1); + } + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode222.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode222.java new file mode 100644 index 0000000..1edf746 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode222.java @@ -0,0 +1,58 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/31 14:57 + * @注释 + */ +public class LeetCode222 { + @Test + public void test() { + TreeNode root = TreeNode.constructTreeByArray(1, 2, 3, 4, 5, 6); + System.out.println(new Solution().countNodes(root)); + } + + class Solution { + /** + * 1. 左右都没有 + * 2. 左有右没有 + * @param root + * @return + */ + public int countNodes(TreeNode root) { + + // 左边深度 + // 右边深度 + // 如果左右边深度不一样,往右边下去找 + // 如果左右深度一样,直接返回 2< System.out.println(i) + ); + } + + class Solution { + + List list; + List path; + public List binaryTreePaths(TreeNode root) { + list = new ArrayList<>(); + path = new ArrayList<>(); + in(root); + return list; + } + + public void in(TreeNode root) { + if (root == null) { + return; + } + if (root.right == null && root.left == null) { + // 还有尾巴一个节点要处理 + StringBuilder stringBuilder = new StringBuilder(); + path.forEach(i -> { + stringBuilder.append(i).append("->"); + }); + stringBuilder.append(root.val); + list.add(stringBuilder.toString()); + return; + } + path.add(root.val); + if (root.left != null) { + in(root.left); + } + if (root.right != null) { + in(root.right); + } + path.remove(path.size() - 1); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode77.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode77.java new file mode 100644 index 0000000..e139940 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode77.java @@ -0,0 +1,57 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/31 15:13 + * @注释 + */ +public class LeetCode77 { + + @Test + public void test() + { + Solution solution = new Solution(); + List> lists = solution.combine(4, 2); + System.out.println(lists); + } + + class Solution { + + List path = new ArrayList<>(); + List> res = new ArrayList<>(); + public List> combine(int n, int k) { + backtrack(n, k, 1); + return res; + } + + public void backtrack(int n, int k, int startIndex) + { + if (path.size() == k) { + res.add(new ArrayList<>(path)); + return; + } +// if (startIndex > n) { +// return; +// } + + for (int i = startIndex; i <= n; i++) { + // 剪枝 + if (n - startIndex + 1 < k - path.size()) { + return; + } + path.add(i); + backtrack(n, k, i + 1); + path.remove(path.size() - 1); + } + + + + } + } +}