This commit is contained in:
whai 2024-01-27 21:29:01 +08:00
parent 3c1d53ac10
commit adeb1985d8
6 changed files with 400 additions and 12 deletions

View File

@ -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<Integer, Integer> 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+左边的数量+1index ~ end
// 中序 左边的数量+1index ~ end
node.right = build(
preorder,
preorderStart + leftChildNumber + 1,
preorderEnd,
inorder,
indexInInorder + 1,
inorderEnd);
return node;
}
}
}

View File

@ -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<Integer, Integer> map = new HashMap<Integer, Integer>();
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;
}
}
}

View File

@ -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<TreeNode> 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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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<String> list = new Solution1().binaryTreePaths(treeNode);
list.forEach(
s -> System.out.println(s)
);
}
class Solution {
List<String> res = new ArrayList<>();
public List<String> 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<String> res = new ArrayList<>();
List<Integer> paths = new ArrayList<>();
public List<String> 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);
}
}
}
}