This commit is contained in:
whai 2024-03-31 15:58:42 +08:00
parent c4248f2cfe
commit 1044629a5e
11 changed files with 542 additions and 1 deletions

View File

@ -1,7 +1,6 @@
package cn.whaifree.leetCode.model; package cn.whaifree.leetCode.model;
import java.util.*; import java.util.*;
import java.util.function.Consumer;
/** /**
* @version 1.0 * @version 1.0
@ -43,6 +42,13 @@ public class TreeNode {
// } // }
// } // }
@Override
public String toString() {
printTree(this);
return null;
}
public static List<Integer> treeToArray(TreeNode root) { public static List<Integer> treeToArray(TreeNode root) {
List<Integer> result = new ArrayList<>(); List<Integer> result = new ArrayList<>();
if (root == null) if (root == null)

View File

@ -5,5 +5,12 @@
[LeetCode15.java](LeetCode15.java) [LeetCode15.java](LeetCode15.java)
水平遍历 [LeetCode94_145_144_102.java](LeetCode94_145_144_102.java) 水平遍历 [LeetCode94_145_144_102.java](LeetCode94_145_144_102.java)
[LeetCode199.java](LeetCode199.java)
[LeetCode105.java](LeetCode105.java) 构造二叉树

View File

@ -0,0 +1,78 @@
package cn.whaifree.redo.redoAll;
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/3/31 11:53
* @注释
*/
public class LeetCode101 {
@Test
public void test()
{
System.out.println(new Solution1().isSymmetric(TreeNode.constructTreeByArray(1,2,3)));
}
class Solution {
public boolean isSymmetric(TreeNode root) {
return isSyn(root.left, root.right);
}
public boolean isSyn(TreeNode one, TreeNode two) {
if (one == null && two == null) {
return true;
} else if (one == null || two == null) {
return false;
} else if (one.val != two.val) {
return false;
}
boolean a = isSyn(one.left, two.right);
boolean b = isSyn(one.right, two.left);
return a && b;
}
}
class Solution1 {
public boolean isSymmetric(TreeNode root) {
if (root.left == null && root.right == null) {
return true;
}else if (root.left == null || root.right == null) {
return false;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.add(root.left);
deque.add(root.right);
while (!deque.isEmpty()) {
TreeNode one = deque.pop();
TreeNode two = deque.pop();
if (one != null && two == null) {
return false;
} else if (one == null && two != null) {
return false;
} else if (one == null && two == null) {
continue;
}
if (one.val != two.val) {
return false;
}
deque.add(one.left);
deque.add(two.right);
deque.add(one.right);
deque.add(two.left);
}
return true;
}
}
}

View File

@ -0,0 +1,55 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/31 13:32
* @注释
*/
public class LeetCode105 {
@Test
public void test() {
Solution solution = new Solution();
int[] preorder = {3, 9, 20, 15, 7};
int[] inorder = {9, 3, 15, 20, 7};
TreeNode treeNode = solution.buildTree(preorder, inorder);
treeNode.printTree();
}
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return build(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1);
}
/**
* preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
*/
public TreeNode build(
int[] preorder,
int[] inorder,
int preStart,
int preEnd,
int inStart,
int inEnd
) {
if (preStart > preEnd || inStart > inEnd) {
return null;
}
int head = preorder[preStart];
TreeNode h = new TreeNode(head);
int inS = 0; // 标记中序中在哪
while (inorder[inS] != head) {
inS++;
}
int l = inS - inStart; // 左边有几个元素
h.left = build(preorder, inorder, preStart + 1, preStart + l, inStart, inS - 1);
h.right = build(preorder, inorder, preStart + l + 1, preEnd, inS + 1, inEnd);
return h;
}
}
}

View File

@ -0,0 +1,42 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/31 12:29
* @注释
*/
public class LeetCode110{
@Test
public void test()
{
Solution solution = new Solution();
System.out.println(solution.isBalanced(TreeNode.constructTreeByArray(1,2,2,3,null,null,3,4,null,null,4)));
}
class Solution {
public boolean isBalanced(TreeNode root) {
return getLevel(root) != -1;
}
public int getLevel(TreeNode root) {
if (root == null) {
return 0;
}
int left = getLevel(root.left);
int right = getLevel(root.right);
if (left == -1 || right == -1) {
return -1;
}
if (Math.abs(left - right) > 1) {
return -1;
}
return Math.max(left, right) + 1;
}
}
}

View File

@ -0,0 +1,68 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/31 11:37
* @注释
*/
public class LeetCode199 {
@Test
public void test() {
Solution1 solution = new Solution1();
List<Integer> integers = solution.rightSideView(TreeNode.constructTreeByArray(1, 2, 3, null, 5, null, null));
for (int i = 0; i < integers.size(); i++) {
System.out.println(integers.get(i));
}
}
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
// 层次遍历每层的最后一个
Deque<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size ; i++) {
TreeNode e = queue.pop();
if (e.left != null) queue.add(e.left);
if (e.right != null) queue.add(e.right);
if (i == size - 1) {
res.add(e.val);
}
}
}
return res;
}
}
class Solution1 {
List<Integer> res = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
level(root, 0);
return res;
}
public void level(TreeNode root, int level) {
if (root == null) {
return;
}
if (res.size() == level) res.add(root.val);
level(root.right, level + 1);
level(root.left, level + 1);
}
}
}

View File

@ -0,0 +1,57 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/31 15:51
* @注释
*/
public class LeetCode216
{
@Test
public void test()
{
Solution solution = new Solution();
List<List<Integer>> lists = solution.combinationSum3(2, 18);
System.out.println(lists);
}
class Solution {
List<List<Integer>> res;
List<Integer> path;
int sum = 0;
public List<List<Integer>> combinationSum3(int k, int n) {
res = new ArrayList<>();
path = new ArrayList<>();
backTracking(k, n, 1);
return res;
}
public void backTracking(int k, int n, int startIndex)
{
if (k == path.size() && n == sum) {
res.add(new ArrayList<>(path));
}
for (int i = startIndex; i < 10; i++) {
if (sum + i > n) {
break;
}
sum += i;
path.add(i);
backTracking(k, n, i + 1);
sum -= i;
path.remove(path.size() - 1);
}
}
}
}

View File

@ -0,0 +1,58 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/31 14:57
* @注释
*/
public class LeetCode222 {
@Test
public void test() {
TreeNode root = TreeNode.constructTreeByArray(1, 2, 3, 4, 5, 6);
System.out.println(new Solution().countNodes(root));
}
class Solution {
/**
* 1. 左右都没有
* 2. 左有右没有
* @param root
* @return
*/
public int countNodes(TreeNode root) {
// 左边深度
// 右边深度
// 如果左右边深度不一样往右边下去找
// 如果左右深度一样直接返回 2<<level - 1
return cir(root);
}
public int cir(TreeNode root) {
if (root == null) {
return 0;
}
TreeNode leftIndex = root;
int leftLevel = 0;
while (leftIndex != null) {
leftIndex = leftIndex.left;
leftLevel++;
}
TreeNode rightIndex = root;
int rightLevel = 0;
while (rightIndex != null) {
rightIndex = rightIndex.right;
rightLevel++;
}
if (leftLevel != rightLevel) {
return cir(root.right) + cir(root.left) + 1;
} else {
return (2 << (leftLevel - 1)) - 1;
}
}
}
}

View File

@ -0,0 +1,54 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/31 15:46
* @注释
*/
public class LeetCode246 {
@Test
public void test()
{
TreeNode root = TreeNode.constructTreeByArray(6,2,8,0,4,7,9,null,null,3,5);
TreeNode p = new TreeNode(0);
TreeNode q = new TreeNode(5);
Solution solution = new Solution();
TreeNode treeNode = solution.lowestCommonAncestor(root, p, q);
System.out.println(treeNode.val);
}
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (root.val == p.val || root.val == q.val) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(left == null && right == null){
return null;
}
if (left != null && right != null) {
return root;
}
if (left == null) {
return right;
}else {
return left;
}
}
}
}

View File

@ -0,0 +1,59 @@
package cn.whaifree.redo.redoAll;
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/3/31 12:54
* @注释
*/
public class LeetCode257 {
@Test
public void test() {
new Solution().binaryTreePaths(TreeNode.constructTreeByArray(1)).forEach(
i -> System.out.println(i)
);
}
class Solution {
List<String> list;
List<Integer> path;
public List<String> binaryTreePaths(TreeNode root) {
list = new ArrayList<>();
path = new ArrayList<>();
in(root);
return list;
}
public void in(TreeNode root) {
if (root == null) {
return;
}
if (root.right == null && root.left == null) {
// 还有尾巴一个节点要处理
StringBuilder stringBuilder = new StringBuilder();
path.forEach(i -> {
stringBuilder.append(i).append("->");
});
stringBuilder.append(root.val);
list.add(stringBuilder.toString());
return;
}
path.add(root.val);
if (root.left != null) {
in(root.left);
}
if (root.right != null) {
in(root.right);
}
path.remove(path.size() - 1);
}
}
}

View File

@ -0,0 +1,57 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/3/31 15:13
* @注释
*/
public class LeetCode77 {
@Test
public void test()
{
Solution solution = new Solution();
List<List<Integer>> lists = solution.combine(4, 2);
System.out.println(lists);
}
class Solution {
List<Integer> path = new ArrayList<>();
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
backtrack(n, k, 1);
return res;
}
public void backtrack(int n, int k, int startIndex)
{
if (path.size() == k) {
res.add(new ArrayList<>(path));
return;
}
// if (startIndex > n) {
// return;
// }
for (int i = startIndex; i <= n; i++) {
// 剪枝
if (n - startIndex + 1 < k - path.size()) {
return;
}
path.add(i);
backtrack(n, k, i + 1);
path.remove(path.size() - 1);
}
}
}
}