diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode17_12BiNode.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode17_12BiNode.java new file mode 100644 index 0000000..0b9eba0 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode17_12BiNode.java @@ -0,0 +1,68 @@ +package cn.whaifree.leetCode.Tree; + +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/28 19:41 + * @注释 + */ +public class LeetCode17_12BiNode { + + @Test + public void test() { + TreeNode root = TreeNode.constructTreeByArray(4, 2, 5, 1, 3, null, 6, 0); + TreeNode treeNode = new Solution().convertBiNode(root); + TreeNode.treeToArray(treeNode).forEach(i -> System.out.println(i)); + + } + class Solution { + + TreeNode res = new TreeNode(); + TreeNode index = res; + public TreeNode convertBiNode(TreeNode root) { + circle(root); + return res.right; + } + + private void circle(TreeNode root) { + if (root == null) { + return; + } + circle(root.left); + index.right = new TreeNode(root.val); + index = index.right; + circle(root.right); + } + } + + class Solution1 { + + TreeNode res = new TreeNode(); + TreeNode index = res; + public TreeNode convertBiNode(TreeNode root) { + circle(root); + return res.right; + } + + private void circle(TreeNode root) { + if (root == null) { + return; + } + circle(root.left); + // 直接使用,不创建新对象,能提升很多效率 + index.right = root; + root.left = null; + index = index.right; + circle(root.right); + } + } +} + + + diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode530.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode530.java new file mode 100644 index 0000000..1fb2f7e --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode530.java @@ -0,0 +1,112 @@ +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文海 + * @Date 2024/1/28 18:44 + * @注释 + */ +public class LeetCode530 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTreeByArray(236,104,701,null,227,null,911); + System.out.println(new Solution2().getMinimumDifference(treeNode)); + } + + class Solution { + + int res = Integer.MAX_VALUE; + public int getMinimumDifference(TreeNode root) { + difference(root); + return res; + } + + public void difference(TreeNode root) { + //二叉树搜索树,差值为相邻节点 + if (root == null) { + return; + } + int right = Integer.MAX_VALUE; + if (root.right != null) { + right = Math.abs(root.val - root.right.val); + } + int left = Integer.MAX_VALUE; + if (root.left != null) { + left = Math.abs(root.val - root.left.val); + } + + if (right < left && right < res) { + res = right; + } else if (left <= right && left < res) { + res = left; + } + difference(root.right); + difference(root.left); + } + } + + class Solution1 { + + int res = Integer.MAX_VALUE; + int storage = Integer.MAX_VALUE; + public int getMinimumDifference(TreeNode root) { + difference(root); + return res; + } + + public void difference(TreeNode root) { + if (root == null) { + return; + } + difference(root.left); + int i = Math.abs(storage - root.val); + if (i < res) { + res = i; + } + // 记录当前这个值,进入下次递归使用 + storage = root.val; + difference(root.right); + } + } + + + + class Solution2 { + + + public int getMinimumDifference(TreeNode root) { + + Deque deque = new LinkedList<>(); + deque.push(root); + int res = Integer.MAX_VALUE; + TreeNode before = null; + while (!deque.isEmpty()) { + TreeNode pop = deque.pop(); + if (pop != null) { + if (pop.right != null) deque.push(pop.right); + deque.push(pop); + deque.push(null); + if (pop.left != null) deque.push(pop.left); + } else { + TreeNode s = deque.pop(); + if (before != null) { + res = Math.min(res, s.val - before.val); + } + before = s; + System.out.println(s.val); + } + } + return res; + + } + + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode617.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode617.java new file mode 100644 index 0000000..79ed48c --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode617.java @@ -0,0 +1,109 @@ +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/28 15:07 + * @注释 + */ +public class LeetCode617 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTreeByArray(1, 3, 2, 5); + TreeNode treeNode1 = TreeNode.constructTreeByArray(2, 1, 3, null, 4, null, 7); + new Solution2().mergeTrees(treeNode, treeNode1).printTree(); + } + + class Solution { + + public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { + return merge(root1, root2); + } + + public TreeNode merge(TreeNode root1, TreeNode root2) { + if (root1 == null && root2 == null) { + return null; + } else if (root1 != null && root2 == null) { + TreeNode treeNode = new TreeNode(root1.val, root1.left, root1.right); + return treeNode; + } else if (root2 != null && root1 == null) { + TreeNode treeNode = new TreeNode(root2.val, root2.left, root2.right); + return treeNode; + } + TreeNode treeNode = new TreeNode(root1.val + root2.val); + treeNode.right = merge(root1.right, root2.right); + treeNode.left = merge(root1.left, root2.left); + return treeNode; + } + } + + class Solution1 { + + public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { + return merge(root1, root2); + } + + public TreeNode merge(TreeNode root1, TreeNode root2) { + // 其中一边为空,直接返回另一半 + if (root2 == null) { + return root1; + } + if (root1 == null) { + return root2; + } + + root1.val = root1.val + root2.val; + root1.right = merge(root1.right, root2.right); + root1.left = merge(root1.left, root2.left); + return root1; + } + } + + class Solution2 { + + public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { + + if (root1 == null) { + return root2; + } + if (root2 == null) { + return root1; + } + + Deque deque = new java.util.ArrayDeque<>(); + deque.add(root1); + deque.add(root2); + while (!deque.isEmpty()) { + TreeNode pop1 = deque.pop(); + TreeNode pop2 = deque.pop(); + + pop1.val += pop2.val; + + // 出来的两个节点,判断左右是否为空 + if (pop1.left != null && pop2.left != null) { + deque.add(pop1.left); + deque.add(pop2.left); + } + if (pop1.right != null && pop2.right != null) { + deque.add(pop1.right); + deque.add(pop2.right); + } + if (pop1.left == null && pop2.left != null) { + pop1.left = pop2.left; + } + if (pop1.right == null && pop2.right != null) { + pop1.right = pop2.right; + } + } + + return root1; + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode654.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode654.java new file mode 100644 index 0000000..041c242 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode654.java @@ -0,0 +1,51 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import jdk.nashorn.internal.runtime.RewriteException; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/28 13:56 + * @注释 + */ +public class LeetCode654 { + + @Test + public void test() { + new Solution().constructMaximumBinaryTree(new int[]{3, 2, 1, 6, 0, 5}).printTree(); + } + + class Solution { + public TreeNode constructMaximumBinaryTree(int[] nums) { + return circle(nums, 0, nums.length-1); + } + + private TreeNode circle(int[] nums, int start, int end) { + if (start > end) { + return null; + } + // 只有一个元素,直接构造即可 + if (end == start) { + return new TreeNode(nums[end]); + } + int maxIndex = findMax(nums, start, end); + TreeNode treeNode = new TreeNode(nums[maxIndex]); + treeNode.left = circle(nums, start, maxIndex - 1); + treeNode.right = circle(nums, maxIndex + 1, end); + return treeNode; + } + + private int findMax(int[] nums, int start, int end) { + int maxIndex = start; + while (start < end) { + start++; + if (nums[start] > nums[maxIndex]) { + maxIndex = start; + } + } + return maxIndex; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode700.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode700.java new file mode 100644 index 0000000..5915eca --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode700.java @@ -0,0 +1,49 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/28 15:53 + * @注释 + */ +public class LeetCode700 { + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTreeByArray(4, 2, 7, 1, 3); + new Solution1().searchBST(treeNode, 2).printTree(); + } + + class Solution { + public TreeNode searchBST(TreeNode root, int val) { + TreeNode index = root; + while (index != null) { + if (index.val > val) { + index = index.left; + } else if (index.val < val) { + index = index.right; + } else if (index.val == val) { + return index; + } + } + return null; + } + } + + class Solution1 { + public TreeNode searchBST(TreeNode root, int val) { + if (root == null) { + return null; + } + if (root.val > val) { + return searchBST(root.left, val); + } + if (root.val < val) { + return searchBST(root.right, val); + } + return root; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode98.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode98.java new file mode 100644 index 0000000..a7d0d9a --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode98.java @@ -0,0 +1,61 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.Node; +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/28 16:05 + * @注释 + */ +public class LeetCode98 { + + + @Test + public void test() { + System.out.println(new Solution().isValidBST(TreeNode.constructTreeByArray(5,4,6,null,null,3,7))); + } + + class Solution { + + + long compare = Long.MIN_VALUE; + boolean res = true; + + public boolean isValidBST(TreeNode root) { + isValid(root); + return res; + } + public void isValid(TreeNode root) { + + if (root == null) { + return; + } + isValid(root.left); + if (root.val <= compare) { + res = false; + return; + } + compare = root.val; + isValid(root.right); + } + + } + + class Solution1 { + public boolean isValidBST(TreeNode root) { + return isValid(root, Integer.MIN_VALUE, Integer.MAX_VALUE); + } + public boolean isValid(TreeNode root,int min,int max) { + if (root == null) { + return true; + } + if (root.val <= min || root.val >= max) { + return false; + } + return isValid(root.left, min, root.val) && isValid(root.right, root.val, max); + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java index 116f0a5..10e79c1 100644 --- a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java +++ b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java @@ -43,6 +43,33 @@ public class TreeNode { // } // } + public static List treeToArray(TreeNode root) { + List result = new ArrayList<>(); + if (root == null) + return result; + + Queue queue = new LinkedList<>(); + queue.offer(root); + + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + if (node != null) { + result.add(node.val); + queue.offer(node.left); + queue.offer(node.right); + } else { + result.add(null); + } + } + + // Remove trailing null values from the end + while (!result.isEmpty() && result.get(result.size() - 1) == null) { + result.remove(result.size() - 1); + } + + return result; + } + public static int getTreeDepth(TreeNode root) { if (root == null) { return 0; @@ -145,6 +172,15 @@ public class TreeNode { return constructTree(objects); } + public static TreeNode constructTreeByArray(Integer... s) { + ArrayList ints = new ArrayList<>(); + for (Integer integer : s) { + ints.add(integer); + } + Integer[] is = ints.toArray(s); + return constructTree(is); + } + public static TreeNode constructTree(Integer[] array) { if (array == null || array.length == 0 || array[0] == null) { return null; diff --git a/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode105_106.java b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode105_106.java new file mode 100644 index 0000000..6af8bcf --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_1_27/LeetCode105_106.java @@ -0,0 +1,106 @@ +package cn.whaifree.redo.redo_24_1_27; + +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/28 13:01 + * @注释 + */ +public class LeetCode105_106 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{3,9,20,null,null,15,7}); + + + new SolutionPost().buildTree(new int[]{9,3,15,20,7}, new int[]{9,15,7,20,3}).printTree(); + } + + class SolutionPre { + 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); + } + + private TreeNode build( + int[] preorder, + int preStart, + int preEnd, + int[] inorder, + int inStart, + int inEnd + ) { + + if (preStart > preEnd || inStart > inEnd) { + return null; + } + int needValue = preorder[preStart]; + Integer indexInInorder = map.get(needValue); + TreeNode treeNode = new TreeNode(needValue); + //treeNode左边节点个数 + int leftNumber = indexInInorder - inStart; + + treeNode.left = build(preorder, preStart + 1, preStart + leftNumber, inorder,inStart,indexInInorder-1); + + treeNode.right = build(preorder, preStart + leftNumber + 1, preEnd, inorder, indexInInorder + 1, inEnd); + + + return treeNode; + } + + } + + + class SolutionPost { + Map map =new HashMap<>(); + + public TreeNode buildTree(int[] inorder, int[] postorder) { + for (int i = 0; i < inorder.length; i++) { + map.put(inorder[i], i); + } + return build(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1); + } + + private TreeNode build( + int[] inorder, + int inStart, + int inEnd, + int[] postorder, + int postStart, + int postEnd + ) { + + // 9 15 8 20 3 + // 9 3 20 15 7 + + if (postStart > postEnd || inStart > inEnd) { + return null; + } + int needValue = postorder[postEnd]; + Integer indexInInorder = map.get(needValue); + TreeNode treeNode = new TreeNode(needValue); + + //treeNode左边节点个数 + int leftNumber = indexInInorder - inStart; + + // 4 5 2 7 3 1 + // 4 2 5 1 3 7 + treeNode.left = build(inorder, inStart, indexInInorder - 1, postorder, postStart, postStart + leftNumber - 1); + treeNode.right = build(inorder, indexInInorder + 1, inEnd, postorder, postStart + leftNumber, postEnd - 1); + + + return treeNode; + } + + } +} 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 index 1429630..c939954 100644 --- 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 @@ -41,8 +41,9 @@ public class LeetCode222 { // 计算两边的深度,如果深度一样,就是完全二叉树,返回 2^0+2^1+2^2... // 满二叉树 if (rightDepth == leftDepth) { - // 相当于 2^0+2^1+....+2^rightDepth - return (2 << rightDepth) - 1; + // 为满二叉树,满二叉树的节点个数 + // 等价于 2 << rightDepth -1 + return (int) Math.pow(2, rightDepth + 1) - 1; } // 如果两边深度不一样,返回+1