各种层次遍历衍生

This commit is contained in:
whai 2024-01-19 14:18:57 +08:00
parent c6ec4950ea
commit 99db282c5d
4 changed files with 304 additions and 0 deletions

View File

@ -0,0 +1,92 @@
package cn.whaifree.leetCode.Tree;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/18 16:11
* @注释
*/
public class LeetCode116 {
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
public void setLeft(Node left) {
this.left = left;
}
public void setRight(Node right) {
this.right = right;
}
};
@Test
public void test() {
Node node = new Node(1);
node.left = new Node(2);
node.right = new Node(3);
node.left.left = new Node(4);
node.left.right = new Node(5);
node.right.left = new Node(6);
node.right.right = new Node(7);
System.out.println(new Solution().connect(node));
}
class Solution {
public Node connect(Node root) {
if (root == null) {
return null;
}
Deque<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node pop = queue.pop();
// 防止把下一层的节点赋值为next
if (i < size - 1) {
pop.next = queue.peek();
}
if (pop.left != null) {
queue.add(pop.left);
}
if (pop.right != null) {
queue.add(pop.right);
}
}
}
return root;
}
}
}

View File

@ -0,0 +1,102 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.Node;
import org.junit.Test;
import java.util.*;
import java.util.function.Consumer;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/18 10:05
* @注释
*/
public class LeetCode429 {
@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));
List<List<Integer>> lists = new Solution1().levelOrder(node);
System.out.println(lists);
}
class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new LinkedList<>();
if (root == null) {
return res;
}
Deque<Node> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> e = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node pop = queue.pop();
e.add(pop.val);
List<Node> children = pop.children;
if (children != null && !children.isEmpty()) {
children.forEach(new Consumer<Node>() {
@Override
public void accept(Node node) {
queue.add(node);
}
});
}
}
res.add(e);
}
return res;
}
}
/**
* 递归实现
*/
class Solution1 {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> levelOrder(Node root) {
if (root == null) {
return res;
}
level(root, 0);
return res;
}
void level(Node root, int level) {
if (root == null) {
return;
}
// 没有对应的层数值就创建
if (res.size() <= level) {
res.add(new ArrayList<>());
}
List<Integer> e = res.get(level);
e.add(root.val);
List<Node> children = root.children;
if (children != null) {
children.forEach(
node -> level(node, level + 1)
);
}
}
}
}

View File

@ -0,0 +1,56 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.Node;
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/18 10:45
* @注释
*/
public class LeetCode515 {
@Test
public void test() {
TreeNode treeNode = TreeNode.constructTree(new Integer[]{3, 1, 5, 0, 2, 4, 6, null, null, null, 3});
treeNode.printTree();
List<Integer> integers = new Solution().largestValues(treeNode);
System.out.println(integers);
}
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if (root == null) {
return res;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode pop = queue.pop();
max = Math.max(pop.val, max);
if (pop.left != null) queue.add(pop.left);
if (pop.right != null) queue.add(pop.right);
}
res.add(max);
}
return res;
}
}
}

View File

@ -0,0 +1,54 @@
package cn.whaifree.leetCode.model;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/18 10:06
* @注释
*/
public class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
public static Node buildTree(Integer[] array) {
if (array == null || array.length == 0 || array[0] == null) {
return null;
}
Node root = new Node(array[0]);
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
int index = 1;
while (!queue.isEmpty() && index < array.length) {
Node current = queue.poll();
while (index < array.length && array[index] != null) {
Node child = new Node(array[index]);
current.children.add(child);
queue.offer(child);
index++;
}
index++;
}
return root;
}
};