各种层次遍历衍生

This commit is contained in:
whai 2024-01-18 10:04:56 +08:00
parent a3f6650e10
commit c6ec4950ea
4 changed files with 301 additions and 1 deletions

View File

@ -19,7 +19,9 @@ public class LeetCode102 {
@Test @Test
public void 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 { class Solution {
@ -47,4 +49,42 @@ public class LeetCode102 {
return res; return res;
} }
} }
class Solution1 {
List<List<Integer>> res = new LinkedList<>();
/**
* 二叉树递归层次遍历
* @param root
* @return
*/
public List<List<Integer>> 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<Integer> 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);
}
}
} }

View File

@ -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<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new LinkedList<>();
if (root == null) {
return res;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> 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<List<Integer>> res = new LinkedList<>();
/**
* 二叉树递归层次遍历
* @param root
* @return
*/
public List<List<Integer>> 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);
}
}
}

View File

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

View File

@ -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<Double> averageOfLevels(TreeNode root) {
List<Double> res = new ArrayList<>();
Deque<TreeNode> 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<Double> res = new ArrayList<>();
List<Integer> count = new ArrayList<>();
public List<Double> 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);
}
}
}
}