diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode116.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode116.java new file mode 100644 index 0000000..dd15b9b --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode116.java @@ -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 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; + + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode429.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode429.java new file mode 100644 index 0000000..4439cb0 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode429.java @@ -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> lists = new Solution1().levelOrder(node); + System.out.println(lists); + + } + + class Solution { + public List> levelOrder(Node 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 ArrayList<>(); + for (int i = 0; i < size; i++) { + Node pop = queue.pop(); + e.add(pop.val); + List children = pop.children; + if (children != null && !children.isEmpty()) { + children.forEach(new Consumer() { + @Override + public void accept(Node node) { + queue.add(node); + } + }); + } + } + res.add(e); + + } + return res; + } + } + + /** + * 递归实现 + */ + class Solution1 { + List> res = new ArrayList<>(); + public List> 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 e = res.get(level); + e.add(root.val); + List children = root.children; + if (children != null) { + children.forEach( + node -> level(node, level + 1) + ); + } + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode515.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode515.java new file mode 100644 index 0000000..bda7fa6 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode515.java @@ -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 integers = new Solution().largestValues(treeNode); + System.out.println(integers); + } + + class Solution { + public List largestValues(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(); + 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; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/model/Node.java b/src/main/java/cn/whaifree/leetCode/model/Node.java new file mode 100644 index 0000000..deee682 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/model/Node.java @@ -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 children; + + public Node() {} + + public Node(int _val) { + val = _val; + } + + public Node(int _val, List _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 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; + } +};