提交 6f372933 编写于 作者: GreyZeng's avatar GreyZeng

level travel bst

上级 cf40a01d
......@@ -36,7 +36,8 @@
[二叉树的按层遍历相关问题](https://www.cnblogs.com/greyzeng/p/16356829.html)
- 类似问题一: 二叉树自底向上层序遍历
- 类似问题二:以数组的形式返回每一层节点的平均值
- 类似问题三:填充每个节点的下一个右侧节点指针
## 更多
......
package git.snippet.leetcode;
package git.snippet.tree;
import java.util.LinkedList;
import java.util.Queue;
// https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/description/
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/
// 本质是树的按层遍历
// 1. hash表+LinkedList
// 2. 仅用LinkedList
......@@ -16,7 +16,7 @@ import java.util.Queue;
// 笔记:https://www.cnblogs.com/greyzeng/p/16356829.html
public class LeetCode_0116_PopulatingNextRightPointersInEachNode {
// 使用LinkedList作为队列,空间O(N)
public static Node connect(Node root) {
public Node connect(Node root) {
if (null == root) {
return null;
}
......@@ -28,6 +28,7 @@ public class LeetCode_0116_PopulatingNextRightPointersInEachNode {
size = queue.size();
// 把每一层用链表串好,pre记录每一层的最后那个位置
// 用于和下一层的开头连接
// 每次把一层搞定
for (; size > 0; size--) {
Node p = queue.poll();
if (pre != null) {
......@@ -46,7 +47,7 @@ public class LeetCode_0116_PopulatingNextRightPointersInEachNode {
return root;
}
public static Node connect2(Node root) {
public Node connect2(Node root) {
if (root == null) {
return null;
}
......
package git.snippet.leetcode;
package git.snippet.tree;
// 和116题一样
// https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
// 笔记:https://www.cnblogs.com/greyzeng/p/16356829.html
public class LeetCode_0117_PopulatingNextRightPointersInEachNodeII {
public static Node connect(Node root) {
// 采用自定义队列
public Node connect(Node root) {
if (root == null) {
return null;
}
......@@ -33,7 +34,7 @@ public class LeetCode_0117_PopulatingNextRightPointersInEachNodeII {
return root;
}
public static class Node {
public class Node {
public int val;
public Node left;
public Node right;
......
/*
* Given a non-empty binary tree, return the average value of the nodes on each level in the form of
* an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average
* value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5,
* 11].
*/
package git.snippet.leetcode;
package git.snippet.tree;
import java.util.ArrayList;
import java.util.LinkedList;
......@@ -12,10 +6,10 @@ import java.util.List;
import java.util.Queue;
// 笔记:https://www.cnblogs.com/greyzeng/p/16356829.html
// https://leetcode.cn/problems/average-of-levels-in-binary-tree/description/
// https://leetcode.com/problems/average-of-levels-in-binary-tree/description/
// 按层遍历,然后求每层的平均值
public class LeetCode_0637_AverageOfLevelsInBinaryTree {
public static List<Double> averageOfLevels(TreeNode root) {
public List<Double> averageOfLevels(TreeNode root) {
if (null == root) {
return new ArrayList<>();
}
......@@ -23,33 +17,32 @@ public class LeetCode_0637_AverageOfLevelsInBinaryTree {
Queue<TreeNode> queue = new LinkedList<>();
TreeNode curEnd = root;
TreeNode nextEnd = null;
int numOfNodes = 0;
Double sumOfNodesVal = 0d;
int numOfNodes = 0; // 记录每一层的节点个数
double sumOfNodesVal = 0d; // 记录每一层的节点平均值(累计值除以个数)
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode poll = queue.poll();
if (null != poll.left) {
queue.offer(poll.left);
nextEnd = poll.left;
TreeNode cur = queue.poll();
if (null != cur.left) {
queue.offer(cur.left);
nextEnd = cur.left;
}
if (null != poll.right) {
queue.offer(poll.right);
nextEnd = poll.right;
if (null != cur.right) {
queue.offer(cur.right);
nextEnd = cur.right;
}
numOfNodes++;
sumOfNodesVal += poll.val;
if (poll == curEnd) {
sumOfNodesVal += cur.val;
if (cur == curEnd) {
result.add(sumOfNodesVal / numOfNodes);
sumOfNodesVal = 0d;
numOfNodes = 0;
curEnd = nextEnd;
}
}
return result;
}
public static class TreeNode {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册