diff --git a/pom.xml b/pom.xml index 78cce23..edaae99 100644 --- a/pom.xml +++ b/pom.xml @@ -14,21 +14,21 @@ - + - - cn.hutool - hutool-http - 5.8.18 - + + cn.hutool + hutool-http + 5.8.18 + - - junit - junit - 4.12 - - + + junit + junit + 4.12 + + diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode101.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode101.java new file mode 100644 index 0000000..47c3b53 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode101.java @@ -0,0 +1,91 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海104 + * @Date 2024/1/21 19:19 + * @注释 + */ +public class LeetCode101 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 2, 3, null,null, 3}); + treeNode.printTree(); + System.out.println(new Solution1().isSymmetric(treeNode)); + + } + + class Solution { + public boolean isSymmetric(TreeNode root) { + if (root == null) { + return true; + } + return compare(root.left, root.right); + } + + public boolean compare(TreeNode left, TreeNode right) { + // left.left 与 right.right递归 + // left.right 与 right.left递归 + if (left == null && right == null) { + return true; + } else if (left != null && right == null) { + return false; + } else if (left == null && right != null) { + return false; + } else if (left.val != right.val) { + return false; + } + + boolean outSite = compare(left.left, right.right); + boolean inSite = compare(left.right, right.left); + return outSite && inSite; + } + } + + class Solution1 { + public boolean isSymmetric(TreeNode root) { + if (root == null) { + return true; + } + + Deque deque = new LinkedList<>(); + deque.add(root.left); + deque.add(root.right); + while (!deque.isEmpty()) { + TreeNode left = deque.pop(); + TreeNode right = deque.pop(); + if (left == null && right == null) { + continue; + } +// else if (left == null && right != null) { +// return false; +// } else if (left != null && right == null) { +// return false; +// } else if (left.val != right.val) { +// return false; +// } + if (left == null || right == null || left.val != right.val) { + return false; + } + + deque.add(left.left); + deque.add(right.right); + deque.add(left.right); + deque.add(right.left); + + } + return true; + } + + + + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode104.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode104.java new file mode 100644 index 0000000..e29b14e --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode104.java @@ -0,0 +1,99 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayDeque; +import java.util.Deque; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/21 20:05 + * @注释 + */ +public class LeetCode104 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 2, 3, null,null,null}); + treeNode.printTree(); + System.out.println(new Solution2().maxDepth(treeNode)); + } + + class Solution { + + int depth = 0; + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + level(root, 1); + return depth; + } + + public void level(TreeNode treeNode, int level) { + if (treeNode == null) { + return; + } + if (level > depth) { + depth = level; + } + if (treeNode.right != null) { + level(treeNode.right, level + 1); + } + if (treeNode.left != null) { + level(treeNode.left, level + 1); + } + + } + } + + class Solution2{ + + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + return level(root); + } + + public int level(TreeNode treeNode) { + if (treeNode == null) { + return 0; + } + int left = level(treeNode.left) + 1; + int right = level(treeNode.right) + 1; + return Math.max(left, right); + } + } + + class Solution1 { + + + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + int depth = 0; + // 右视图 + Deque deque = new ArrayDeque<>(); + deque.add(root); + while (!deque.isEmpty()) { + int size = deque.size(); + for (int i = 0; i < size; i++) { + TreeNode pop = deque.pop(); + if (pop.left != null) { + deque.add(pop.left); + } + if (pop.right != null) { + deque.add(pop.right); + } + } + depth++; + } + return depth; + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode111.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode111.java new file mode 100644 index 0000000..aa927ef --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode111.java @@ -0,0 +1,107 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayDeque; +import java.util.Deque; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/21 20:36 + * @注释 + */ +public class LeetCode111 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 2, 3, null,null,null,3}); + treeNode.printTree(); + System.out.println(new Solution2().minDepth(treeNode)); + } + + class Solution { + int minDepth = Integer.MAX_VALUE; + public int minDepth(TreeNode root) { + if (root == null) { + return 0; + } + getDepth(root, 1); + return minDepth; + } + + void getDepth(TreeNode root, int level) { + if (root == null) { + return; + } + if (root.left != null) { + getDepth(root.left, level + 1); + } + if (root.right != null) { + getDepth(root.right, level + 1); + } + // 左右子树都为空,才表明已经到底部了 + if (root.left == null && root.right == null && level < minDepth) { + minDepth = level; + } + } + } + + class Solution1 { + public int minDepth(TreeNode root) { + return getDepth(root); + } + + int getDepth(TreeNode root) { + if (root == null) { + return 0; + } + int right = getDepth(root.right) ; + int left = getDepth(root.left) ; + if (root.left == null && root.right != null) { + return right + 1; + } + if (root.right == null && root.left != null) { + return left + 1; + } + + return 1 + Math.min(right, left); + } + } + + + class Solution2 { + public int minDepth(TreeNode root) { + if (root == null) { + return 0; + } + + int depth = 0; + + // 右视图 + Deque deque = new ArrayDeque<>(); + deque.add(root); + while (!deque.isEmpty()) { + int size = deque.size(); + depth++; + for (int i = 0; i < size; i++) { + TreeNode pop = deque.pop(); + if (pop.left != null) { + deque.add(pop.left); + } + if (pop.right != null) { + deque.add(pop.right); + } + if (pop.right == null && pop.left == null) { + // 从上到下第一个双节点非空就是输出 + return depth; + } + } + + } + return 0; + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode559.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode559.java new file mode 100644 index 0000000..05a517c --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode559.java @@ -0,0 +1,79 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.Node; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/21 20:20 + * @注释 + */ +public class LeetCode559 { + + @Test + public void test() { + Node node = new Node(1); + node.children = new LinkedList<>(); + node.children.add(new Node(2)); + node.children.add(new Node(3)); + node.children.add(new Node(4)); + node.children.get(2).children = new ArrayList<>(); + node.children.get(2).children.add(new Node(6)); + node.children.get(2).children.add(new Node(7)); + + System.out.println(new Solution1().maxDepth(node)); + } + + class Solution { + public int maxDepth(Node root) { + return getDepth(root); + } + + public int getDepth(Node root) { + if (root == null) { + return 0; + } + int max = 1; + + List children = root.children; + if (children != null) { + for (Node child : children) { + max = Math.max(getDepth(child) + 1, max); + } + } + return max; + } + } + + class Solution1 { + int max = 0; + public int maxDepth(Node root) { + if (root == null) { + return 0; + } + getDepth(root, 1); + return max; + } + + public void getDepth(Node root,int level) { + if (root == null) { + return; + } + if (level > max) { + max = level; + } + List children = root.children; + if (children != null) { + for (Node child : children) { + getDepth(child, level + 1); + } + } + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_20/LeetCode199.java b/src/main/java/cn/whaifree/redo/redo_24_1_20/LeetCode199.java new file mode 100644 index 0000000..e4b51ca --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_20/LeetCode199.java @@ -0,0 +1,71 @@ +package cn.whaifree.redo.redo_24_1_20; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/20 19:01 + * @注释 + */ +public class LeetCode199 { + @Test + public void test() { + TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, null, 5, null, 4}); + root.printTree(); + List integers = new Solution1().rightSideView(root); + System.out.println(integers); + } + + 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 pop = queue.pop(); + if (i == size - 1) { + res.add(pop.val); + } + if (pop.left!=null) queue.add(pop.left); + if (pop.right!=null) queue.add(pop.right); + } + } + return res; + + } + } + + class Solution1 { + + List res = new LinkedList(); + + public List rightSideView(TreeNode root) { + level(root, 0); + return res; + } + + void level(TreeNode root,int level) { + if (root == null) { + return; + } + + if (level == res.size()) { + res.add(root.val); + } + if (root.right != null) level(root.right, level + 1); + if (root.left != null) level(root.left, level + 1); + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_20/LeetCode94.java b/src/main/java/cn/whaifree/redo/redo_24_1_20/LeetCode94.java new file mode 100644 index 0000000..1c77e6a --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_20/LeetCode94.java @@ -0,0 +1,83 @@ +package cn.whaifree.redo.redo_24_1_20; + +import cn.whaifree.leetCode.LinkedList.LeetCode142; +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/20 18:37 + * @注释 + */ +public class LeetCode94 { + + @Test + public void test() { + TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4}); + root.printTree(); + System.out.println(new Solution().inorderTraversal(root)); + } + + class Solution{ + public List inorderTraversal(TreeNode root) { + + List res = new LinkedList<>(); + if (root == null) { + return res; + } + + Deque stack = new LinkedList<>(); + stack.push(root); + while (!stack.isEmpty()) { + + TreeNode pop = stack.pop(); + if (pop != null) { + // 中序 +// stack.push(pop); +// stack.push(null); +// if (pop.left != null) { +// stack.push(pop.left); +// } +// if (pop.right != null) { +// stack.push(pop.right); +// } + + // 后序 再Reverse + if (pop.left != null) { + stack.push(pop.left); + } + if (pop.right != null) { + stack.push(pop.right); + } + stack.push(pop); + stack.push(null); + // 前序 +// if (pop.right != null) { +// stack.push(pop.right); +// } +// if (pop.left != null) { +// stack.push(pop.left); +// } +// stack.push(pop); +// stack.push(null); + + } else { + // 前序 中序 +// res.add(stack.pop().val); + // 后序 + res.add(0,stack.pop().val); + } + + } + return res; + + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode15.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode15.java similarity index 97% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode15.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode15.java index 819a35e..db60d2a 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode15.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode15.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode151.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode151.java similarity index 98% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode151.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode151.java index 9b7f444..718c66e 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode151.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode151.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode18.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode18.java similarity index 98% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode18.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode18.java index 88932d3..f259c33 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode18.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode18.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode202.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode202.java similarity index 95% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode202.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode202.java index 335742a..c128ee6 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode202.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode202.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode203_false.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode203_false.java similarity index 96% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode203_false.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode203_false.java index cd13648..a192cd1 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode203_false.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode203_false.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import cn.whaifree.leetCode.model.ListNode; diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode206_false.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode206_false.java similarity index 95% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode206_false.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode206_false.java index c41bc00..8e7140c 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode206_false.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode206_false.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import cn.whaifree.leetCode.model.ListNode; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode209_false.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode209_false.java similarity index 95% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode209_false.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode209_false.java index 8bd8503..c64cf5a 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode209_false.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode209_false.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode2487_false.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode2487_false.java similarity index 97% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode2487_false.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode2487_false.java index fec578f..58fb5ec 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode2487_false.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode2487_false.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import cn.whaifree.leetCode.model.ListNode; import org.junit.Test; diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode287_false.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode287_false.java similarity index 89% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode287_false.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode287_false.java index 1562743..5825718 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode287_false.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode287_false.java @@ -1,11 +1,7 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; -import cn.hutool.core.lang.hash.Hash; import org.junit.Test; -import java.util.HashSet; -import java.util.Set; - public class LeetCode287_false { @Test diff --git a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode28_false.java b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode28_false.java similarity index 94% rename from src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode28_false.java rename to src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode28_false.java index 9848359..3c7acd8 100644 --- a/src/main/java/cn/whaifree/redo/redo24_1_7/LeetCode28_false.java +++ b/src/main/java/cn/whaifree/redo/redo_24_1_7/LeetCode28_false.java @@ -1,4 +1,4 @@ -package cn.whaifree.redo.redo24_1_7; +package cn.whaifree.redo.redo_24_1_7; import org.junit.Test;