From ca386eaae03e0cc99dea1742884fd4ad93fcfbd3 Mon Sep 17 00:00:00 2001 From: whai Date: Wed, 24 Jan 2024 22:10:12 +0800 Subject: [PATCH] Tree --- .../whaifree/leetCode/Tree/LeetCode112.java | 79 +++++++++++++++ .../whaifree/leetCode/Tree/LeetCode113.java | 62 ++++++++++++ .../whaifree/leetCode/Tree/LeetCode404.java | 96 +++++++++++++++++++ .../whaifree/leetCode/Tree/LeetCode513.java | 77 +++++++++++++++ .../cn/whaifree/leetCode/model/TreeNode.java | 13 +++ 5 files changed, 327 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode112.java create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode113.java create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode404.java create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode513.java diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode112.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode112.java new file mode 100644 index 0000000..bdb65e1 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode112.java @@ -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 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; + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode113.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode113.java new file mode 100644 index 0000000..78761f3 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode113.java @@ -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> res = new ArrayList<>(); + public List> 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 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); + } + + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode404.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode404.java new file mode 100644 index 0000000..e108bbc --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode404.java @@ -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 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 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; + } + } +} + diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode513.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode513.java new file mode 100644 index 0000000..9b95c14 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode513.java @@ -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> e = new ArrayList<>(); + Deque 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); + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java index d66d9d1..a8be111 100644 --- a/src/main/java/cn/whaifree/leetCode/model/TreeNode.java +++ b/src/main/java/cn/whaifree/leetCode/model/TreeNode.java @@ -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 tree = new ArrayList(); + 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;