From c6ec4950ea921c6f1dc6b28a5509857f6cdd5dda Mon Sep 17 00:00:00 2001 From: whai Date: Thu, 18 Jan 2024 10:04:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=84=E7=A7=8D=E5=B1=82=E6=AC=A1=E9=81=8D?= =?UTF-8?q?=E5=8E=86=E8=A1=8D=E7=94=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/Tree/LeetCode102.java | 42 ++++++++- .../whaifree/leetCode/Tree/LeetCode107.java | 86 ++++++++++++++++++ .../whaifree/leetCode/Tree/LeetCode199.java | 87 +++++++++++++++++++ .../whaifree/leetCode/Tree/LeetCode637.java | 87 +++++++++++++++++++ 4 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode107.java create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode199.java create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode637.java diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java index 55d3df4..0530400 100644 --- a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java @@ -19,7 +19,9 @@ public class LeetCode102 { @Test public void test() { - System.out.println(new Solution().levelOrder(TreeNode.constructTree(new Integer[]{3,9,20,null,null,15,7}))); + TreeNode root = TreeNode.constructTree(new Integer[]{3, 9, 20, null, null, 15, 7}); + root.printTree(); + System.out.println(new Solution1().levelOrder(root)); } class Solution { @@ -47,4 +49,42 @@ public class LeetCode102 { return res; } } + + class Solution1 { + + List> res = new LinkedList<>(); + + /** + * 二叉树递归层次遍历 + * @param root + * @return + */ + public List> levelOrder(TreeNode root) { + + if (root == null) { + return res; + } + level(root, 0); + + return res; + } + + public void level(TreeNode root, int level) { + if (root == null) { + return; + } + List i = null; + if (res.size() <= level) { + i = new ArrayList<>(); + res.add(level, i); + } else { + i = res.get(level); + } + i.add(root.val); + + level(root.left, level + 1); + level(root.right, level + 1); + + } + } } diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode107.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode107.java new file mode 100644 index 0000000..ff0354f --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode107.java @@ -0,0 +1,86 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import com.sun.org.apache.bcel.internal.generic.NEW; +import org.junit.Test; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/17 14:50 + * @注释 + */ +public class LeetCode107 { + + @Test + public void test() { + TreeNode root = TreeNode.constructTree(new Integer[]{3,9,20,null,null,15,7}); + root.printTree(); + + System.out.println(new Solution1().levelOrderBottom(root)); + } + class Solution { + public List> levelOrderBottom(TreeNode root) { + List> res = new LinkedList<>(); + if (root == null) { + return res; + } + Deque queue = new LinkedList<>(); + + queue.add(root); + while (!queue.isEmpty()) { + int size = queue.size(); + List e = new LinkedList<>(); + for (int i = 0; i < size; i++) { + TreeNode pop = queue.pop(); + e.add(pop.val); + if (pop.left != null) { + queue.add(pop.left); + } + if (pop.right != null) { + queue.add(pop.right); + } + } + // 每次都采用头插法 + res.add(0,e); + } + return res; + } + } + + class Solution1 { + + List> res = new LinkedList<>(); + + /** + * 二叉树递归层次遍历 + * @param root + * @return + */ + public List> levelOrderBottom(TreeNode root) { + + if (root == null) { + return res; + } + level(root, 0); + Collections.reverse(res); + return res; + } + + public void level(TreeNode root, int level) { + if (root == null) { + return; + } + int size = res.size(); + if (size <= level) { + res.add(new ArrayList<>()); + } + res.get(level).add(root.val); + level(root.left, level + 1); + level(root.right, level + 1); + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode199.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode199.java new file mode 100644 index 0000000..9b4d999 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode199.java @@ -0,0 +1,87 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.*; +import java.util.function.Consumer; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/17 15:07 + * @注释 + */ +public class LeetCode199 { + + @Test + public void test() { + TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4}); + root.printTree(); + System.out.println(new Solution1().rightSideView(root)); + } + + + + class Solution { + /** + * 层次遍历,每层最右边就是结果 + * @param root + * @return + */ + public List rightSideView(TreeNode root) { + List res = new ArrayList<>(); + if (root == null) { + return res; + } + + Deque queue = new LinkedList<>(); + + 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 res = new ArrayList<>(); + public List rightSideView(TreeNode root) { + if (root == null) { + return res; + } + 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); + + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode637.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode637.java new file mode 100644 index 0000000..c1c5579 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode637.java @@ -0,0 +1,87 @@ +package cn.whaifree.leetCode.Tree; + +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; +import java.util.function.Consumer; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/17 16:41 + * @注释 + */ +public class LeetCode637 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1,2,3}); + System.out.println(new Solution1().averageOfLevels(treeNode)); + } + + class Solution { + + + public List averageOfLevels(TreeNode root) { + + List res = new ArrayList<>(); + Deque queue = new LinkedList<>(); + queue.add(root); + + while (!queue.isEmpty()) { + + int size = queue.size(); + double sum = 0; + for (int i = 0; i < size; i++) { + TreeNode pop = queue.pop(); + sum += pop.val; + if(pop.left!=null) queue.add(pop.left); + if(pop.right!=null) queue.add(pop.right); + } + res.add((sum / size)); + } + return res; + } + } + + + class Solution1 { + + List res = new ArrayList<>(); + List count = new ArrayList<>(); + + + public List averageOfLevels(TreeNode root) { + + level(root, 0); + for (int i = 0; i < res.size(); i++) { + res.set(i, res.get(i) / count.get(i)); + } + return res; + } + + void level(TreeNode treeNode, int level) { + + if (treeNode == null) { + return; + } + + if (res.size() <= level) { + res.add(0d); + count.add(0); + } + res.set(level, res.get(level) + treeNode.val); + count.set(level, count.get(level) + 1); + if (treeNode.left != null) { + level(treeNode.left, level + 1); + } + if (treeNode.right != null) { + level(treeNode.right, level + 1); + } + } + } +}