一些二叉树
This commit is contained in:
whai 2024-01-21 22:05:25 +08:00
parent 3247b3f654
commit f469f18748
17 changed files with 552 additions and 26 deletions

24
pom.xml
View File

@ -14,21 +14,21 @@
</properties>
<dependencies>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.8.18</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.8.18</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>

View File

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

View File

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

View File

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

View File

@ -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<Node> 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<Node> children = root.children;
if (children != null) {
for (Node child : children) {
getDepth(child, level + 1);
}
}
}
}
}

View File

@ -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<Integer> integers = new Solution1().rightSideView(root);
System.out.println(integers);
}
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 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<Integer> res = new LinkedList<Integer>();
public List<Integer> 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);
}
}
}

View File

@ -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<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
if (root == null) {
return res;
}
Deque<TreeNode> 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;
}
}
}

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo.redo24_1_7;
package cn.whaifree.redo.redo_24_1_7;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo.redo24_1_7;
package cn.whaifree.redo.redo_24_1_7;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo.redo24_1_7;
package cn.whaifree.redo.redo_24_1_7;
import org.junit.Test;

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo.redo24_1_7;
package cn.whaifree.redo.redo_24_1_7;
import org.junit.Test;

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo.redo24_1_7;
package cn.whaifree.redo.redo_24_1_7;
import org.junit.Test;

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package cn.whaifree.redo.redo24_1_7;
package cn.whaifree.redo.redo_24_1_7;
import org.junit.Test;