From adeb1985d8a0c4b156a8a411b9afe61d5cf44d1a Mon Sep 17 00:00:00 2001 From: whai Date: Sat, 27 Jan 2024 21:29:01 +0800 Subject: [PATCH] bn --- .../whaifree/leetCode/Tree/LeetCode105.java | 87 +++++++++++++++++++ .../whaifree/leetCode/Tree/LeetCode106.java | 59 ++++++++++--- .../redo/redo_24_1_27/LeetCode101.java | 83 ++++++++++++++++++ .../redo/redo_24_1_27/LeetCode110.java | 46 ++++++++++ .../redo/redo_24_1_27/LeetCode222.java | 53 +++++++++++ .../redo/redo_24_1_27/LeetCode257.java | 84 ++++++++++++++++++ 6 files changed, 400 insertions(+), 12 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode105.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode101.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode110.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode222.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode257.java diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode105.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode105.java new file mode 100644 index 0000000..2cbd55a --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode105.java @@ -0,0 +1,87 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/27 13:34 + * @注释 + */ +public class LeetCode105 { + + @Test + public void test() { + TreeNode.constructTree(new Integer[]{3,9,20,1,null,15,7}); + // 9 3 20 7 15 + // 20 3 7 9 15 + // 20 7 3 15 9 + + + new Solution().buildTree(new int[]{3,9,1,20,15,7}, new int[]{1,9,3,15,20,7}).printTree(); + + } + + class Solution { + Map map = new HashMap<>(); + public TreeNode buildTree(int[] preorder, int[] inorder) { + for (int i = 0; i < inorder.length; i++) { + map.put(inorder[i], i); + } + return build(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1); + } + + public TreeNode build( + int[] preorder, + int preorderStart, + int preorderEnd, + int[] inorder, + int inorderStart, + int inorderEnd + ) { + if (preorderStart > preorderEnd || inorderStart > inorderEnd) { + return null; + } + int value = preorder[preorderStart]; + Integer indexInInorder = map.get(value); + TreeNode node = new TreeNode(value); + // 左边的个数就是index的InInorderNumber + + //只要有左边节点数量,其他都好算 + int leftChildNumber = indexInInorder - inorderStart; + + // 前序 start的下一个 ~ start+左边的数量 + // 中序 start ~ start+左边的数量-1(不包含index) + node.left = build( + preorder, + preorderStart + 1, + preorderStart + leftChildNumber, + inorder, + inorderStart, + inorderStart + leftChildNumber - 1 + ); + + //3 9 1 20 15 7 + //1 9 3 15 20 7 + + // 前序 start+左边的数量+1(index) ~ end + // 中序 左边的数量+1(index) ~ end + node.right = build( + preorder, + preorderStart + leftChildNumber + 1, + preorderEnd, + inorder, + indexInInorder + 1, + inorderEnd); + + return node; + + } + } + + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode106.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode106.java index 55d4f76..1066ae4 100644 --- a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode106.java +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode106.java @@ -3,6 +3,11 @@ package cn.whaifree.leetCode.Tree; import cn.whaifree.leetCode.model.TreeNode; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * @version 1.0 * @Author whai文海 @@ -17,39 +22,69 @@ public class LeetCode106 { // 9 3 20 7 15 // 20 3 7 9 15 // 20 7 3 15 9 + + //9,3,15,20,7 + //9,15,7,20,3 new Solution().buildTree(new int[]{9,3,15,20,7}, new int[]{9,15,7,20,3}).printTree(); } class Solution { + Map map = new HashMap(); public TreeNode buildTree(int[] inorder, int[] postorder) { if (inorder.length == 0) { return null; } - return circle(inorder, postorder, 0, inorder.length - 1, postorder.length - 1); + for (int i = 0; i < inorder.length; i++) { + map.put(inorder[i], i); + } + return circle(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1); } + public TreeNode circle( int[] inorder, - int[] postorder, int inorderStart, int inorderEnd, - int poster + int[] postorder, + int posterStart, + int posterEnd ) { - if (poster < 0 || inorderStart > inorderEnd) { + if (posterEnd < posterStart || inorderStart > inorderEnd) { return null; } - int lastValue = postorder[poster]; - // 中序的指针,左边为左树,右边为右树。 - int index = 0; - while (inorder[index] != lastValue) { - index++; - } + int lastValue = postorder[posterEnd]; TreeNode root = new TreeNode(lastValue); - root.right = circle(inorder, postorder, index + 1, inorderEnd, index+inorderEnd-2); - root.left = circle(inorder, postorder, inorderStart, index - 1,inorderStart+index-1 ); + + // 中序中的下标 + Integer index = map.get(lastValue); + + // 左字树的个数 + + int leftChileNumber = index - inorderStart; + // 中序 start ~ start+leftnumber(即index) + // 后序 start ~ start+左边的树 + root.left = circle( + inorder, + inorderStart, + index - 1, + postorder, + posterStart, + posterStart + leftChileNumber - 1); + // 中序 index+1 ~ end + // 后序 start+left ~ end-1(中序节点) + root.right = circle( + inorder, + index + 1, + inorderEnd, postorder, + posterStart + leftChileNumber, + posterEnd - 1); return root; } } + + + + } diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode101.java b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode101.java new file mode 100644 index 0000000..9543cc4 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode101.java @@ -0,0 +1,83 @@ +package cn.whaifree.redo.redo_24_1_27; + +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/1/27 16:53 + * @注释 + */ +public class LeetCode101 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 2, 3, null,null, 3}); + 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 ATree,TreeNode BTree) { + if (ATree== null && BTree == null) { + return true; + }else if(ATree != null && BTree == null) { + return false; + }else if (ATree == null && BTree != null) { + return false; + } else if (ATree.val != BTree.val) { + return false; + } + + boolean compare1 = compare(ATree.left, BTree.right); + boolean compare2 = compare(ATree.right, BTree.left); + return compare1 && compare2; + } + } + + class Solution1 { + public boolean isSymmetric(TreeNode root) { + if (root == null) { + return true; + } + Deque deque = new LinkedList<>(); + deque.add(root.right); + deque.add(root.left); + while (!deque.isEmpty()) { + TreeNode a = deque.pop(); + TreeNode b = deque.pop(); + if (a== null && b == null) { + continue; + }else if(a != null && b == null) { + return false; + }else if (a == null && b != null) { + return false; + } else if (a.val != b.val) { + return false; + } + deque.add(a.right); + deque.add(b.left); + deque.add(a.left); + deque.add(b.right); + } + + return true; + } + + + + + } + + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode110.java b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode110.java new file mode 100644 index 0000000..3b177c6 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode110.java @@ -0,0 +1,46 @@ +package cn.whaifree.redo.redo_24_1_27; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/27 17:21 + * @注释 + */ +public class LeetCode110 { + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1,2,2,3,null,null,3,4,null,null,4}); + + System.out.println(new Solution().isBalanced(treeNode)); + } + + + class Solution { + public boolean isBalanced(TreeNode root) { + if (root == null) { + return true; + } + return level(root) != -1; + } + + public int level(TreeNode root) { + if (root == null) { + return 0; + } + // 不要在这里+1,容易混淆 -1(如果子树不平衡)+1=0,在最下面返回值里层数+1 + int right = level(root.right); + int left = level(root.left); + // 一旦有不平衡,要立刻返回 + if (right==-1||left==-1) return -1; + + if (Math.abs(right - left) > 1) { + return -1; + } + return Math.max(right, left) + 1; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode222.java b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode222.java new file mode 100644 index 0000000..1429630 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode222.java @@ -0,0 +1,53 @@ +package cn.whaifree.redo.redo_24_1_27; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/27 20:57 + * @注释 + */ +public class LeetCode222 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5, 6}); + System.out.println(new Solution().countNodes(treeNode)); + } + + + class Solution { + public int countNodes(TreeNode root) { + if (root == null) { + return 0; + } + + TreeNode indexRight = root.right; + int rightDepth = 0; + while (indexRight != null) { + rightDepth++; + indexRight = indexRight.right; + } + TreeNode indexLeft = root.left; + int leftDepth = 0; + while (indexLeft != null) { + leftDepth++; + indexLeft = indexLeft.left; + } + + + // 计算两边的深度,如果深度一样,就是完全二叉树,返回 2^0+2^1+2^2... + // 满二叉树 + if (rightDepth == leftDepth) { + // 相当于 2^0+2^1+....+2^rightDepth + return (2 << rightDepth) - 1; + } + + // 如果两边深度不一样,返回+1 + return countNodes(root.right) + countNodes(root.left) + 1; + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode257.java b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode257.java new file mode 100644 index 0000000..7bd17bd --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode257.java @@ -0,0 +1,84 @@ +package cn.whaifree.redo.redo_24_1_27; + +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/1/27 19:52 + * @注释 + */ +public class LeetCode257 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 3, null, 5}); + List list = new Solution1().binaryTreePaths(treeNode); + list.forEach( + s -> System.out.println(s) + ); + + } + + class Solution { + List res = new ArrayList<>(); + public List binaryTreePaths(TreeNode root) { + level(root, ""); + return res; + } + + public void level(TreeNode root, String path) { + if (root == null) { + return; + } + if (root.left == null && root.right == null) { + String s = new StringBuilder().append(path).append(root.val).toString(); + res.add(s); + return; + } + String s = new StringBuilder().append(path).append(root.val).append("->").toString(); + level(root.right, s); + level(root.left, s); + } + } + + class Solution1 { + + List res = new ArrayList<>(); + List paths = new ArrayList<>(); + public List binaryTreePaths(TreeNode root) { + level(root); + return res; + } + + public void level(TreeNode root) { + if (root == null) { + return; + } + paths.add(root.val); + if (root.left == null && root.right == null) { + StringBuilder str = new StringBuilder(); + for (int i = 0; i < paths.size()-1; i++) { + str.append(paths.get(i)).append("->"); + } + str.append(root.val); + res.add(str.toString()); + return; + } + + + if (root.right != null) { + level(root.right); + paths.remove(paths.size() - 1); + } + if (root.left != null) { + level(root.left); + paths.remove(paths.size() - 1); + } + } + } +}