diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode17.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode17.java new file mode 100644 index 0000000..29cb0f7 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode17.java @@ -0,0 +1,70 @@ +package cn.whaifree.leetCode.BackTracking; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/2 10:31 + * @注释 + */ +public class LeetCode17 { + + @Test + public void test() { + String digits = ""; + new Solution().letterCombinations(digits).forEach(s -> System.out.println(s)); + } + + class Solution { + List map = new ArrayList<>(); + + List result = new ArrayList<>(); + + StringBuilder s = new StringBuilder(); + + public List letterCombinations(String digits) { + if (digits.length() == 0) { + return result; + } + map.add(0, null); + map.add(1, null); + map.add(2, "abc"); + map.add(3, "def"); + map.add(4, "ghi"); + map.add(5, "jkl"); + map.add(6, "mno"); + map.add(7, "pqrs"); + map.add(8, "tuv"); + map.add(9, "wxyz"); + backTracking(digits, 0, digits.length()); + return result; + } + + /** + * + * @param digits 原始字符 23 + * @param number 使用到第几个字符 + * @param needLength 需要几个字符,等价于digits.length + */ + void backTracking(String digits, int number,int needLength) { + if (s.length() == needLength) { + result.add(new String(s.toString())); + return; + } + + int c = digits.charAt(number) - 48; + String sValue = map.get(c); + int length = sValue.length(); + for (int i = 0; i < length; i++) { + char c1 = sValue.charAt(i); + s.append(c1); + backTracking(digits, number + 1, needLength); + s.deleteCharAt(s.length() - 1); + } + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode216.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode216.java new file mode 100644 index 0000000..2f7b01a --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode216.java @@ -0,0 +1,81 @@ +package cn.whaifree.leetCode.BackTracking; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/2 8:22 + * @注释 + */ +public class LeetCode216 { + + @Test + public void test() { + Solution solution = new Solution(); + solution.combinationSum3(9, 45).forEach( + list -> { + System.out.println(list); + } + ); + } + + class Solution { + + List> res = new LinkedList<>(); + List path = new ArrayList<>(); + int sum = 0; + /** + * 相加之和为n的k个数的组合 + * @param k + * @param n + * @return + */ + public List> combinationSum3(int k, int n) { + circle(1, 9, n, k); + return res; + } + + public void circle(int start, int end, int n, int k) { + if (path.size() == k && sum == n) { + res.add(new ArrayList<>(path)); + return; + } + + // 1. 如果sum>n了,证明往后加只会更大,因为都是正数,就不再继续了 + // sum>n + // + // 2. 如果 9个数要9个数 + // 已经选择size + // 还需选择k-size + // 可以选择的数end-start + // 可以选择的数<还需选择的数 end-start n) { + path.remove(path.size() - 1); + sum -= i; + return; + } + circle(i + 1, end, n, k); + path.remove(path.size() - 1); + sum -= i; + } + } + } + + + +} diff --git a/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode77.java b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode77.java new file mode 100644 index 0000000..1524f8c --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/BackTracking/LeetCode77.java @@ -0,0 +1,57 @@ +package cn.whaifree.leetCode.BackTracking; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/31 20:24 + * @注释 + */ +public class LeetCode77 { + + @Test + public void test() { + new Solution().combine(4, 4).forEach( + list -> { + list.forEach(System.out::print); + System.out.println(); + } + ); + } + + + class Solution { + List> res = new LinkedList<>(); + + List path = new ArrayList<>(); + + public List> combine(int n, int k) { + circle(1, n, k); + return res; + } + + void circle(int start, int end, int k) { + if (path.size() == k) { + res.add(new ArrayList<>(path)); + return; + } + + // 剪枝,可以选择的个数不满足最小需要的个数 + // 已经选择的个数 path.size + // 还需要的个数 k-path.size + // 可以选择的个数<还需要的个数 end-ik-path.size时正常执行 i end) { + return null; + } + int middle = (end + start) / 2; + int num = nums[middle]; + TreeNode root = new TreeNode(num); + root.left = construct(nums, start, middle - 1); + root.right = construct(nums, middle + 1, end); + return root; + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode450.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode450.java new file mode 100644 index 0000000..acae216 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode450.java @@ -0,0 +1,157 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/31 9:38 + * @注释 + */ +public class LeetCode450 { + + @Test + public void test() { + new Solution1().deleteNode(TreeNode.constructTreeByArray(5,3,6,2,4,null,7,null,null), 0).printTree(); + + } + + class Solution { + public TreeNode deleteNode(TreeNode root, int key) { + if (root == null) { + return null; + } + + if (root.left == null && root.right == null && root.val == key) { + return null; + } + + + TreeNode pre = null; + TreeNode tmp = root; + while (tmp!=null) { + if (tmp.val > key) { + pre = tmp; + tmp = tmp.left; + } else if (tmp.val < key) { + pre = tmp; + tmp = tmp.right; + } else { + break; + } + } + + // 找不到该要删除的节点 + if (pre != null && pre.val != key && tmp == null) { + return root; + } + + + + // 左右都为空 + // 左为空 + // 右为空 + if (pre.left == tmp) { + if (tmp.right == null && tmp.left == null) { + pre.left = null; + } else if (tmp.left == null) { + pre.left = tmp.right; + } else if (tmp.right == null) { + pre.left = tmp.left; + } + }else if (pre.right == tmp){ + if (tmp.right == null && tmp.left == null) { + pre.right = null; + } else if (tmp.left == null) { + pre.right = tmp.right; + } else if (tmp.right == null) { + pre.right = tmp.left; + } + } + // 左右都不空 + // tmp.left移动到tmp.right.left.....最后 + TreeNode move = tmp.right; + while (move.left != null) { + move = move.left; + } + move.left = tmp.left; + // tmp.right.left移动到tmp的位置 有可能在pre的左边或右边 + if (pre.left == tmp) { + pre.left = tmp.right; + } else { + pre.right = tmp.right; + } + return root; + } + } + + + class Solution1 { + /** + * 递归 + * @param root + * @param key + * @return + */ + public TreeNode deleteNode(TreeNode root, int key) { + + if (root == null) { + return root; + } + if (root.val == key) { + + // 左边为空 返回右边 + // 两边都为空,则返回空,随便返回子节点 + if (root.left == null) { + return root.right; + } + if (root.right == null) { + return root.left; + } + TreeNode index = root.right; + while (index.left != null) { + index = index.left; + } + index.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; + } + } + + /** + * 把二叉树变成链表 + */ + class Solution2 { + TreeNode pre = new TreeNode(); + TreeNode cur = pre; + + public TreeNode deleteNode(TreeNode root, int key) { + search(root, key); + return pre.right; + } + + private void search(TreeNode root, int key) { + if (root == null) { + return; + } + search(root.left, key); + TreeNode r = root.right; + if (root.val != key) { + cur.right = root; + root.left = null; + root.right = null; + cur = cur.right; + } + search(r, key); + + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode538.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode538.java new file mode 100644 index 0000000..399908f --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode538.java @@ -0,0 +1,65 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.Deque; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/31 19:15 + * @注释 + */ +public class LeetCode538 { + + @Test + public void test() { + + new Solution1().convertBST(TreeNode.constructTreeByArray(4, 1, 6, 0, 2, 5, 7, null, null, null, 3, null, null, null, 8)).printTree(); + } + + class Solution { + int sum = 0; + public TreeNode convertBST(TreeNode root) { + if (root == null) { + return null; + } + root.right = convertBST(root.right); + root.val += sum; + sum = root.val; + root.left = convertBST(root.left); + return root; + } + } + + + class Solution1 { + public TreeNode convertBST(TreeNode root) { + + if (root == null) { + return null; + } + + Deque stack = new java.util.LinkedList<>(); + stack.push(root); + + + int sum = 0; + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + if (pop == null) { + TreeNode ans = stack.pop(); + ans.val += sum; + sum = ans.val; + } else { + if (pop.left!=null) stack.push(pop.left); + stack.push(pop); + stack.push(null); + if (pop.right!=null) stack.push(pop.right); + } + } + return root; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode669.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode669.java new file mode 100644 index 0000000..d93ce75 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode669.java @@ -0,0 +1,40 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/31 14:01 + * @注释 + */ +public class LeetCode669 { + + @Test + public void test() { + new Solution().trimBST(TreeNode.constructTreeByArray(3,0,4,null,2,null,null,1), 1, 3).printTree(); + } + + class Solution { + + public TreeNode trimBST(TreeNode root, int low, int high) { + if (root == null) { + return null; + } + + root.left = trimBST(root.left, low, high); + root.right = trimBST(root.right, low, high); + + if (root.val < low) { + return root.right; + } + + if (root.val > high) { + return root.left; + } + + return root; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode701.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode701.java new file mode 100644 index 0000000..fa80ada --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode701.java @@ -0,0 +1,88 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/31 9:13 + * @注释 + */ +public class LeetCode701 { + + @Test + public void test() { + new Solution2().insertIntoBST(TreeNode.constructTreeByArray(5,null,14,10,77,null,null,null,95,null,null), 4).printTree(); + + } + + class Solution { + public TreeNode insertIntoBST(TreeNode root, int val) { + if (root == null) { + return new TreeNode(val); + } + insert(root, val); + return root; + } + + public void insert(TreeNode root, int val) { + if (root == null) { + return; + } + + + if (root.val < val) { + insert(root.right, val); + if (root.right == null) { + root.right = new TreeNode(val); + } + } else { + insert(root.left, val); + if (root.left == null) { + root.left = new TreeNode(val); + } + } + } + } + + class Solution1 { + public TreeNode insertIntoBST(TreeNode root, int val) { + // 找到了叶子节点,该位置就是要插入的地方 + if (root == null) { + return new TreeNode(val); + } + + if (root.val < val) { + root.right = insertIntoBST(root.right, val); + }else if (root.val > val){ + root.left = insertIntoBST(root.left, val); + } + return root; + } + } + class Solution2 { + public TreeNode insertIntoBST(TreeNode root, int val) { + if (root==null) return new TreeNode(val); + TreeNode pre = null; + TreeNode index = root; + while (index != null) { + pre = index; + if (index.val > val) { + index = index.left; + } else { + index = index.right; + } + } + if (pre.val > val) { + pre.left = new TreeNode(val); + } else { + pre.right = new TreeNode(val); + } + return root; + + + } + } + +}