diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode131.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode131.java new file mode 100644 index 0000000..a6ae63d --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode131.java @@ -0,0 +1,50 @@ +package cn.whaifree.redo.redo_24_2_22; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/22 11:25 + * @注释 + */ +public class LeetCode131 { + + @Test + public void test( + ) { + TreeNode treeNode = TreeNode.constructTreeByArray(3, 5, 1, 6, 2, 0, 8, null, null, 7, 4); + } + + class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root == null) { + return root; + } + + if (root.val == p.val) { + return root; + } + if (root.val == q.val) { + return root; + } + + TreeNode right = lowestCommonAncestor(root.right, p, q); + TreeNode left = lowestCommonAncestor(root.left, p, q); + + // 这里用于输出公共父节点 + // 某个节点,第一个出现的左边有p或者q,右边也有p或者q,则就是公共父节点。 + if (right != null && left != null) { + return root; + } + + if (right != null) { + return right; + } else { + return left; + } + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode206.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode206.java new file mode 100644 index 0000000..84295bd --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode206.java @@ -0,0 +1,38 @@ +package cn.whaifree.redo.redo_24_2_22; + +import cn.whaifree.leetCode.model.ListNode; +import cn.whaifree.leetCode.model.TreeNode; +import com.sun.xml.internal.ws.server.sei.ValueGetter; +import org.junit.Test; + +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/22 12:35 + * @注释 + */ +public class LeetCode206 { + + @Test + public void test() { + new Solution().reverseList(ListNode.listNodeFromArray(new int[]{1, 2, 3})).printList(); + } + + + class Solution { + public ListNode reverseList(ListNode head) { + return reverse(null, head); + } + + public ListNode reverse(ListNode before, ListNode after) { + if (after == null) { + return before; + } + ListNode tmp = after.next; + after.next = before; + return reverse(after,tmp); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode222.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode222.java new file mode 100644 index 0000000..8558f78 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode222.java @@ -0,0 +1,44 @@ +package cn.whaifree.redo.redo_24_2_22; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/22 13:16 + * @注释 + */ +public class LeetCode222 { + + @Test + public void test() { + System.out.println(new Solution().countNodes(TreeNode.constructTreeByArray(1))); + } + + class Solution { + public int countNodes(TreeNode root) { + if (root == null) { + return 0; + } + + TreeNode tmp = root; + int right = 0; + while (tmp!=null) { + tmp = tmp.right; + right++; + } + tmp = root; + int left = 0; + while (tmp!=null) { + tmp = tmp.left; + left++; + } + if (right == left) { + return (2 << right-1) - 1; + } + + return countNodes(root.left) + countNodes(root.right) + 1; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode40.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode40.java new file mode 100644 index 0000000..58abe32 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode40.java @@ -0,0 +1,59 @@ +package cn.whaifree.redo.redo_24_2_22; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/22 13:26 + * @注释 + */ +public class LeetCode40 { + + @Test + public void test() { + new Solution().combinationSum2(new int[]{10,1,2,7,6,1,5}, 8).forEach( + list -> { + System.out.println(list); + } + ); + } + class Solution { + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + boolean[] used = null; + public List> combinationSum2(int[] candidates, int target) { + Arrays.sort(candidates); + used = new boolean[candidates.length]; + backTracking(candidates, target, 0); + return res; + } + + public void backTracking(int[] candidates, int retailNeed,int start) { + if (0 == retailNeed ) { + res.add(new ArrayList<>(path)); + return; + } + if (retailNeed < 0) { + return; + } + + for (int i = start; i < candidates.length; i++) { + // 如果前后两个相同,前一个false表示已经使用完了,再使用就会重复 + if (i > 0 && candidates[i - 1] == candidates[i] && used[i - 1] == false) { + continue; + } + path.add(candidates[i]); + used[i] = true; + backTracking(candidates, retailNeed - candidates[i], i + 1); + used[i] = false; + path.remove(path.size() - 1); + } + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode450.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode450.java new file mode 100644 index 0000000..ae87611 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode450.java @@ -0,0 +1,53 @@ +package cn.whaifree.redo.redo_24_2_22; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/22 11:40 + * @注释 + */ +public class LeetCode450 { + + @Test + public void test() { + new Solution().deleteNode(TreeNode.constructTreeByArray(5,3,6,2,4,null,7), 7).printTree(); + } + + class Solution { + + public TreeNode deleteNode(TreeNode root, int key) { + + if (root == null) { + return null; + } + + if (root.val == key) { + // 遇到左边或右边为空 + if (root.left == null) { + return root.right; + } + if (root.right == null) { + return root.left; + } + + // 左边右边都不为空 + TreeNode pre = root.right; + while (pre.left != null) { + pre = pre.left; + } + pre.left = root.left; + return root.right; + } + + if (root.val > key) { + root.left = deleteNode(root.left, key); + } else if (root.val < key) { + root.right = deleteNode(root.right, key); + } + return root; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode501.java b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode501.java new file mode 100644 index 0000000..1c6a1e0 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_2_22/LeetCode501.java @@ -0,0 +1,68 @@ +package cn.whaifree.redo.redo_24_2_22; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/22 12:04 + * @注释 + */ +public class LeetCode501 { + + @Test + public void test() { + TreeNode root = TreeNode.constructTreeByArray(1,null,2); + for (int i : new Solution().findMode(root)) { + System.out.println(i); + } + } + + + class Solution { + List res = new ArrayList<>(); + // 最高出现的次数 + int most = 0; + // 当前临时记录的值 + int tmpValue = Integer.MIN_VALUE; + // 临时记录的值得次数 + int tmpCount = 0; + + public int[] findMode(TreeNode root) { + recursion(root); + int[] ints = new int[res.size()]; + for (int i = 0; i < res.size(); i++) { + ints[i] = res.get(i); + } + return ints; + } + + public void recursion(TreeNode root) { + if (root == null) { + return; + } + recursion(root.left); + if (root.val == tmpValue) { + tmpCount++; + }else{ + tmpValue = root.val; + tmpCount = 1; + } + + if (tmpCount > most) { + most = tmpCount; + res.clear(); + res.add(root.val); + } else if (tmpCount == most) { + res.add(root.val); + } + + + recursion(root.right); + } + } +}