Skip to content

Commit dc05028

Browse files
committed
一刷1104
1 parent 3fbaf79 commit dc05028

File tree

6 files changed

+118
-31
lines changed

6 files changed

+118
-31
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7753,14 +7753,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
77537753
//|{doc_base_url}/1103-distribute-candies-to-people.adoc[题解]
77547754
//|Easy
77557755
//|
7756-
//
7757-
//|{counter:codes}
7758-
//|{leetcode_base_url}/path-in-zigzag-labelled-binary-tree/[1104. Path In Zigzag Labelled Binary Tree^]
7759-
//|{source_base_url}/_1104_PathInZigzagLabelledBinaryTree.java[Java]
7760-
//|{doc_base_url}/1104-path-in-zigzag-labelled-binary-tree.adoc[题解]
7761-
//|Medium
7762-
//|
7763-
//
7756+
7757+
|{counter:codes}
7758+
|{leetcode_base_url}/path-in-zigzag-labelled-binary-tree/[1104. Path In Zigzag Labelled Binary Tree^]
7759+
|{source_base_url}/_1104_PathInZigzagLabelledBinaryTree.java[Java]
7760+
|{doc_base_url}/1104-path-in-zigzag-labelled-binary-tree.adoc[题解]
7761+
|Medium
7762+
|
7763+
77647764
//|{counter:codes}
77657765
//|{leetcode_base_url}/filling-bookcase-shelves/[1105. Filling Bookcase Shelves^]
77667766
//|{source_base_url}/_1105_FillingBookcaseShelves.java[Java]
Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,70 @@
11
[#1104-path-in-zigzag-labelled-binary-tree]
2-
= 1104. Path In Zigzag Labelled Binary Tree
2+
= 1104. 二叉树寻路
33

4-
{leetcode}/problems/path-in-zigzag-labelled-binary-tree/[LeetCode - Path In Zigzag Labelled Binary Tree^]
4+
https://leetcode.cn/problems/path-in-zigzag-labelled-binary-tree/[LeetCode - 1104. 二叉树寻路 ^]
55

6-
In an infinite binary tree where every node has two children, the nodes are labelled in row order.
6+
在一棵无限的二叉树上,每个节点都有两个子节点,树中的节点 *逐行* 依次按 “之” 字形进行标记。
77

8-
In the odd numbered rows (ie., the first, third, fifth,...), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left.
8+
如下图所示,在奇数行(即,第一行、第三行、第五行……)中,按从左到右的顺序进行标记;
99

10-
image::https://assets.leetcode.com/uploads/2019/06/24/tree.png[{image_attr}]
10+
而偶数行(即,第二行、第四行、第六行……)中,按从右到左的顺序进行标记。
1111

12-
Given the `label` of a node in this tree, return the labels in the path from the root of the tree to the node with that `label`.
12+
image::images/1104-01.png[{image_attr}]
1313

14-
15-
*Example 1:*
14+
给你树上某一个节点的标号 `label`,请你返回从根节点到该标号为 `label` 节点的路径,该路径是由途经的节点标号所组成的。
1615

17-
[subs="verbatim,quotes,macros"]
18-
----
19-
*Input:* label = 14
20-
*Output:* [1,3,4,14]
21-
----
16+
*示例 1:*
2217

23-
*Example 2:*
18+
....
19+
输入:label = 14
20+
输出:[1,3,4,14]
21+
....
2422

25-
[subs="verbatim,quotes,macros"]
26-
----
27-
*Input:* label = 26
28-
*Output:* [1,2,6,10,26]
29-
----
23+
*示例 2:*
24+
25+
....
26+
输入:label = 26
27+
输出:[1,2,6,10,26]
28+
....
3029

31-
32-
*Constraints:*
3330

31+
*提示:*
3432

35-
* `1 <= label <= 10^6`
33+
* `1 \<= label \<= 10^6^`
3634
3735
3836
37+
== 思路分析
38+
39+
找到每一层的左右端点的值,确认所求数值在本层中的下标(从 `1` 开始),开始循环,每次循环下标都除以 `2` 再向上取整。这样就可以获取每个数值对应的节点。
40+
41+
看官方题解,每层两端的数值都可以从当前层数获取,不需要单独存储。
3942

4043
[[src-1104]]
44+
[tabs]
45+
====
46+
一刷::
47+
+
48+
--
4149
[{java_src_attr}]
4250
----
4351
include::{sourcedir}/_1104_PathInZigzagLabelledBinaryTree.java[tag=answer]
4452
----
53+
--
54+
55+
// 二刷::
56+
// +
57+
// --
58+
// [{java_src_attr}]
59+
// ----
60+
// include::{sourcedir}/_1104_PathInZigzagLabelledBinaryTree_2.java[tag=answer]
61+
// ----
62+
// --
63+
====
64+
65+
66+
== 参考资料
4567

68+
. https://leetcode.cn/problems/path-in-zigzag-labelled-binary-tree/solutions/901472/er-cha-shu-xun-lu-by-leetcode-solution-ryx0/[1104. 二叉树寻路 - 官方题解^]
69+
. https://leetcode.cn/problems/path-in-zigzag-labelled-binary-tree/solutions/902506/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/[1104. 二叉树寻路 - 一题双解 :「模拟」&「数学」^]
70+
. https://leetcode.cn/problems/path-in-zigzag-labelled-binary-tree/solutions/96636/jian-dan-yi-dong-de-gong-shi-shi-jian-guo-100-by-a/[1104. 二叉树寻路 - 简单易懂的公式 时间过100%^]

docs/images/1104-01.png

52.4 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@ include::1094-car-pooling.adoc[leveloffset=+1]
22912291

22922292
// include::1103-distribute-candies-to-people.adoc[leveloffset=+1]
22932293

2294-
// include::1104-path-in-zigzag-labelled-binary-tree.adoc[leveloffset=+1]
2294+
include::1104-path-in-zigzag-labelled-binary-tree.adoc[leveloffset=+1]
22952295

22962296
// include::1105-filling-bookcase-shelves.adoc[leveloffset=+1]
22972297

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,11 @@ endif::[]
903903
|{doc_base_url}/0986-interval-list-intersections.adoc[题解]
904904
|✅ 双指针。先排除两种没有交集的情况,剩下四种有交集的情况,左边选两个里面最大的,右边选两个里面最小的。哪个小,向右移动哪个的指针。
905905

906+
|{counter:codes2503}
907+
|{leetcode_base_url}/path-in-zigzag-labelled-binary-tree/[1104. 二叉树寻路^]
908+
|{doc_base_url}/1104-path-in-zigzag-labelled-binary-tree.adoc[题解]
909+
|✅ 找到每一层的左右端点的值,确认所求数值在本层中的下标(从 `1` 开始),开始循环,每次循环下标都除以 `2` 再向上取整。这样就可以获取每个数值对应的节点。
910+
906911

907912
|===
908913

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1104_PathInZigzagLabelledBinaryTree {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-06-03 15:52:57
11+
*/
12+
public List<Integer> pathInZigZagTree(int label) {
13+
int cur = label;
14+
int len = 1;
15+
boolean isOdd;
16+
int max = 0;
17+
int depth = ((int) (Math.log(label) / Math.log(2))) + 1;
18+
int[] left = new int[depth];
19+
int[] right = new int[depth];
20+
int level = 1;
21+
while (left[depth - 1] == 0) {
22+
isOdd = (level & 1) == 1;
23+
max += len;
24+
if (isOdd) {
25+
right[level - 1] = max;
26+
left[level - 1] = max - len + 1;
27+
} else {
28+
left[level - 1] = max;
29+
right[level - 1] = max - len + 1;
30+
}
31+
if (cur > len) {
32+
cur -= len;
33+
}
34+
level++;
35+
len *= 2;
36+
}
37+
List<Integer> result = new ArrayList<>();
38+
int index = depth - 1;
39+
boolean isEven = (depth & 1) == 0;
40+
int levelIndex = isEven ? (left[depth - 1] - right[depth - 1] + 1) - cur + 1 : cur;
41+
while (index >= 0) {
42+
if (left[index] < right[index]) {
43+
result.add(left[index] + levelIndex - 1);
44+
} else {
45+
result.add(left[index] - levelIndex + 1);
46+
}
47+
levelIndex = (int) Math.ceil(levelIndex / 2.0);
48+
index--;
49+
}
50+
return result.reversed();
51+
}
52+
// end::answer[]
53+
public static void main(String[] args) {
54+
new _1104_PathInZigzagLabelledBinaryTree()
55+
.pathInZigZagTree(26);
56+
}
57+
}

0 commit comments

Comments
 (0)