parent
5c8a23caeb
commit
fdafcff0ed
@ -0,0 +1,82 @@
|
||||
package class_2023_02_2_week;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
// 给你二叉树的根结点 root ,请你设计算法计算二叉树的 垂序遍历 序列。
|
||||
// 对位于 (row, col) 的每个结点而言,
|
||||
// 其左右子结点分别位于 (row + 1, col - 1) 和 (row + 1, col + 1)
|
||||
// 树的根结点位于 (0, 0) 。
|
||||
// 二叉树的 垂序遍历 从最左边的列开始直到最右边的列结束,按列索引每一列上的所有结点,
|
||||
// 形成一个按出现位置从上到下排序的有序列表。如果同行同列上有多个结点,
|
||||
// 则按结点的值从小到大进行排序。
|
||||
// 返回二叉树的 垂序遍历 序列。
|
||||
// 测试链接 : https://leetcode.cn/problems/vertical-order-traversal-of-a-binary-tree/
|
||||
public class Code02_VerticalOrderTraversalOfBinaryTree {
|
||||
|
||||
// 不提交这个类
|
||||
public static class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
}
|
||||
|
||||
// 提交以下所有的code
|
||||
public static class Info {
|
||||
public int row;
|
||||
public int col;
|
||||
public int val;
|
||||
|
||||
public Info(int r, int c, int v) {
|
||||
row = r;
|
||||
col = c;
|
||||
val = v;
|
||||
}
|
||||
}
|
||||
|
||||
public static class InfoComparator implements Comparator<Info> {
|
||||
|
||||
@Override
|
||||
public int compare(Info o1, Info o2) {
|
||||
if (o1.col != o2.col) {
|
||||
return o1.col - o2.col;
|
||||
}
|
||||
if (o1.row != o2.row) {
|
||||
return o1.row - o2.row;
|
||||
}
|
||||
return o1.val - o2.val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static List<List<Integer>> verticalTraversal(TreeNode root) {
|
||||
ArrayList<Info> collects = new ArrayList<>();
|
||||
Info rootInfo = new Info(0, 0, root.val);
|
||||
collects.add(rootInfo);
|
||||
dfs(root, rootInfo, collects);
|
||||
List<List<Integer>> ans = new ArrayList<>();
|
||||
collects.sort(new InfoComparator());
|
||||
for (int i = 0; i < collects.size(); i++) {
|
||||
if (i == 0 || collects.get(i - 1).col != collects.get(i).col) {
|
||||
ans.add(new ArrayList<>());
|
||||
}
|
||||
ans.get(ans.size() - 1).add(collects.get(i).val);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static void dfs(TreeNode root, Info rootInfo, ArrayList<Info> collects) {
|
||||
if (root.left != null) {
|
||||
Info leftInfo = new Info(rootInfo.row + 1, rootInfo.col - 1, root.left.val);
|
||||
collects.add(leftInfo);
|
||||
dfs(root.left, leftInfo, collects);
|
||||
}
|
||||
if (root.right != null) {
|
||||
Info rightInfo = new Info(rootInfo.row + 1, rootInfo.col + 1, root.right.val);
|
||||
collects.add(rightInfo);
|
||||
dfs(root.right, rightInfo, collects);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package class_2023_02_2_week;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
// 给你一棵二叉树的根节点 root ,返回树的 最大宽度 。
|
||||
// 树的 最大宽度 是所有层中最大的 宽度 。
|
||||
// 每一层的 宽度 被定义为该层最左和最右的非空节点(即,两个端点)之间的长度。
|
||||
// 将这个二叉树视作与满二叉树结构相同,两端点间会出现一些延伸到这一层的 null 节点,
|
||||
// 这些 null 节点也计入长度。
|
||||
// 题目数据保证答案将会在 32 位 带符号整数范围内。
|
||||
// 测试链接 : https://leetcode.cn/problems/maximum-width-of-binary-tree/
|
||||
public class Code03_WidthOfBinaryTree {
|
||||
|
||||
// 不提交这个类
|
||||
public static class TreeNode {
|
||||
public int val;
|
||||
public TreeNode left;
|
||||
public TreeNode right;
|
||||
}
|
||||
|
||||
// 提交以下所有的方法
|
||||
public static class Info {
|
||||
// 当前节点
|
||||
public TreeNode node;
|
||||
// 编号!
|
||||
public int index;
|
||||
|
||||
public Info(TreeNode n, int i) {
|
||||
node = n;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
public static int widthOfBinaryTree(TreeNode root) {
|
||||
int ans = 1;
|
||||
LinkedList<Info> queue = new LinkedList<>();
|
||||
queue.add(new Info(root, 1));
|
||||
while (!queue.isEmpty()) {
|
||||
ans = Math.max(ans, queue.peekLast().index - queue.peekFirst().index + 1);
|
||||
int size = queue.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Info cur = queue.pollFirst();
|
||||
if (cur.node.left != null) {
|
||||
queue.addLast(new Info(cur.node.left, cur.index * 2));
|
||||
}
|
||||
if (cur.node.right != null) {
|
||||
queue.addLast(new Info(cur.node.right, cur.index * 2 + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue