Tree
This commit is contained in:
parent
24c2458f4f
commit
ca386eaae0
79
src/main/java/cn/whaifree/leetCode/Tree/LeetCode112.java
Normal file
79
src/main/java/cn/whaifree/leetCode/Tree/LeetCode112.java
Normal file
@ -0,0 +1,79 @@
|
||||
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/24 20:59
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode112 {
|
||||
|
||||
@Test
|
||||
public void test(
|
||||
) {
|
||||
TreeNode treeNode = TreeNode.constructTree(new Integer[]{5,4,8,11,null,13,4,7,2,null,null,null,1});
|
||||
treeNode.printTree();
|
||||
System.out.println(new Solution1().hasPathSum(treeNode, 22));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public boolean hasPathSum(TreeNode root, int targetSum) {
|
||||
return level(root, 0, targetSum);
|
||||
}
|
||||
|
||||
public boolean level(TreeNode treeNode,int sum,int targetSum) {
|
||||
if (treeNode == null) {
|
||||
return false;
|
||||
}
|
||||
sum += treeNode.val;
|
||||
// 最后一个必须是叶子节点
|
||||
if (treeNode.left == null && treeNode.right == null && sum == targetSum) {
|
||||
return true;
|
||||
}
|
||||
boolean left = level(treeNode.left, sum, targetSum);
|
||||
boolean right = level(treeNode.right, sum, targetSum);
|
||||
return left || right;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
|
||||
/**
|
||||
* 使用队列
|
||||
* @param root
|
||||
* @param targetSum
|
||||
* @return
|
||||
*/
|
||||
public boolean hasPathSum(TreeNode root, int targetSum) {
|
||||
if (root == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Deque<Object> queue = new java.util.ArrayDeque<>();
|
||||
queue.add(root);
|
||||
queue.add(root.val);
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode pop = (TreeNode) queue.pop();
|
||||
Integer value = (Integer) queue.pop();
|
||||
if (pop.right == null && pop.left == null && value == targetSum) {
|
||||
return true;
|
||||
}
|
||||
if (pop.right != null) {
|
||||
queue.add(pop.right);
|
||||
queue.add(value + pop.right.val);
|
||||
}
|
||||
if (pop.left != null) {
|
||||
queue.add(pop.left);
|
||||
queue.add(value + pop.left.val);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
62
src/main/java/cn/whaifree/leetCode/Tree/LeetCode113.java
Normal file
62
src/main/java/cn/whaifree/leetCode/Tree/LeetCode113.java
Normal file
@ -0,0 +1,62 @@
|
||||
package cn.whaifree.leetCode.Tree;
|
||||
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/1/24 21:30
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode113 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
TreeNode treeNode = TreeNode.constructTree(new Integer[]{5,4,8,null,4});
|
||||
treeNode.printTree();
|
||||
System.out.println(new Solution().pathSum(treeNode, 13));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
|
||||
if (root == null) {
|
||||
return res;
|
||||
}
|
||||
level(root, targetSum, new ArrayList<>());
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param root 节点
|
||||
* @param needSum 还需要的数
|
||||
* @param nowHasPath 记录已经走过的路,也用于回退
|
||||
*/
|
||||
public void level(TreeNode root, int needSum,ArrayList<Integer> nowHasPath) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
nowHasPath.add(root.val);
|
||||
if (root.left == null && root.right == null && root.val == needSum) {
|
||||
res.add(new ArrayList<>(nowHasPath));
|
||||
// 回退全部交给上一次递归
|
||||
}
|
||||
if (root.left != null) {
|
||||
level(root.left, needSum - root.val, nowHasPath);
|
||||
nowHasPath.remove(nowHasPath.size() - 1);
|
||||
}
|
||||
if (root.right != null) {
|
||||
level(root.right, needSum - root.val, nowHasPath);
|
||||
nowHasPath.remove(nowHasPath.size() - 1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
96
src/main/java/cn/whaifree/leetCode/Tree/LeetCode404.java
Normal file
96
src/main/java/cn/whaifree/leetCode/Tree/LeetCode404.java
Normal file
@ -0,0 +1,96 @@
|
||||
package cn.whaifree.leetCode.Tree;
|
||||
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import com.sun.org.apache.regexp.internal.RE;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/1/24 19:03
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode404 {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
TreeNode treeNode = TreeNode.constructTree(new Integer[]{3,9,20,null,null,15,7});
|
||||
treeNode.printTree();
|
||||
System.out.println(new Solution2().sumOfLeftLeaves(treeNode));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public int sumOfLeftLeaves(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
int right = sumOfLeftLeaves(root.right);
|
||||
int left = sumOfLeftLeaves(root.left);
|
||||
|
||||
// 如果左边存在,且是叶子节点,再返回
|
||||
if (root.left != null && root.left.left == null && root.left.right == null) {
|
||||
return left + right + root.left.val;
|
||||
}
|
||||
return left + right;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
public int sumOfLeftLeaves(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
Deque<TreeNode> stack = new LinkedList<>();
|
||||
stack.push(root);
|
||||
// 遍历
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode pop = stack.pop();
|
||||
if (pop != null) {
|
||||
// 遇到没进入过的节点
|
||||
if (pop.right!=null) stack.push(pop.right);
|
||||
if (pop.left!=null) stack.push(pop.left);
|
||||
stack.push(pop);
|
||||
stack.push(null);
|
||||
|
||||
} else {
|
||||
TreeNode pop1 = stack.pop();
|
||||
// if (pop1.left.left == null && pop1.left.right == null) {
|
||||
//
|
||||
// }
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution2 {
|
||||
public int sumOfLeftLeaves(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
Deque<TreeNode> deque= new LinkedList<>();
|
||||
deque.add(root);
|
||||
// 遍历
|
||||
int sum = 0;
|
||||
while (!deque.isEmpty()) {
|
||||
TreeNode pop = deque.pop();
|
||||
if (pop.right != null) {
|
||||
deque.add(pop.right);
|
||||
}
|
||||
if (pop.left != null) {
|
||||
// left是叶子节点,并且是左边
|
||||
if (pop.left.right == null && pop.left.left == null) {
|
||||
sum += pop.left.val;
|
||||
}
|
||||
deque.add(pop.left);
|
||||
}
|
||||
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
77
src/main/java/cn/whaifree/leetCode/Tree/LeetCode513.java
Normal file
77
src/main/java/cn/whaifree/leetCode/Tree/LeetCode513.java
Normal file
@ -0,0 +1,77 @@
|
||||
package cn.whaifree.leetCode.Tree;
|
||||
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import javafx.scene.layout.VBox;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/1/24 19:35
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode513 {
|
||||
@Test
|
||||
public void test() {
|
||||
TreeNode root = TreeNode.constructTree(new Integer[]{1});
|
||||
root.printTree();
|
||||
System.out.println(new Solution1().findBottomLeftValue(root));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public int findBottomLeftValue(TreeNode root) {
|
||||
if (root == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ArrayList<List<Integer>> e = new ArrayList<>();
|
||||
Deque<TreeNode> deque = new java.util.LinkedList<>();
|
||||
deque.add(root);
|
||||
int res = 0;
|
||||
while (!deque.isEmpty()) {
|
||||
int size = deque.size();
|
||||
while (size-- > 0) {
|
||||
TreeNode pop = deque.pop();
|
||||
if (pop.right!=null) deque.add(pop.right);
|
||||
if (pop.left!=null) deque.add(pop.left);
|
||||
if (deque.isEmpty()) {
|
||||
res = pop.val;
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class Solution1 {
|
||||
Integer depth = 0;
|
||||
Integer res = 0;
|
||||
public int findBottomLeftValue(TreeNode root) {
|
||||
// 只有一个节点
|
||||
if (root.left==null&&root.right==null) return root.val;
|
||||
level(root, 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
public void level(TreeNode root, int level) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.right == null && root.left == null) {
|
||||
if (level > depth) {
|
||||
depth = level;
|
||||
res = root.val;
|
||||
}
|
||||
// 遇到每层层第一个叶子节点,就可能是叶子节点,如果层数更深,那么就替换
|
||||
return;
|
||||
}
|
||||
level(root.left, level + 1);
|
||||
level(root.right, level + 1);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package cn.whaifree.leetCode.model;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
@ -132,6 +133,18 @@ public class TreeNode {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static TreeNode constructRandom(Integer number) {
|
||||
Random random = new Random();
|
||||
List<Integer> tree = new ArrayList<Integer>();
|
||||
for (int i = 0; i < number; i++) {
|
||||
tree.add(random.nextInt(10));
|
||||
}
|
||||
// 将Arraylist变为数组
|
||||
Integer[] objects = tree.toArray(new Integer[tree.size()]);
|
||||
|
||||
return constructTree(objects);
|
||||
}
|
||||
|
||||
public static TreeNode constructTree(Integer[] array) {
|
||||
if (array == null || array.length == 0 || array[0] == null) {
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user