Skip to content

Commit 4a953b3

Browse files
committed
三刷199
1 parent 226645a commit 4a953b3

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

docs/0199-binary-tree-right-side-view.adoc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[#0199-binary-tree-right-side-view]
22
= 199. 二叉树的右视图
33

4-
https://leetcode.cn/problems/binary-tree-right-side-view/[LeetCode - 199. 二叉树的右视图 ^]
4+
https://leetcode.cn/problems/binary-tree-right-side-view/[LeetCode - 199. 二叉树的右视图^]
55

66
给定一个二叉树的 *根节点* `root`,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
77

@@ -46,6 +46,10 @@ image::images/0199-01.jpg[{image_attr}]
4646

4747
深度优先遍历,按照 `<level, node>` 的格式,把每个节点都放到 `Map` 里,因为是先左后右,所以,每层最后只剩下了最右边的元素。
4848

49+
其实,可以不用 `Map`,直接用 `List` 即可。不过,要注意**先根遍历**和**中根遍历**的区别:先根遍历每层都是按顺序从上到下添加到 `List`;而中根遍历,每次都是最下层到,所以,要添加 `null` 占位符把 `List` 给撑起来,后续直接按照坐标设置。
50+
51+
另外,仔细体会一下“左中右遍历”和“右中左遍历”的区别。
52+
4953
[[src-0199]]
5054
[tabs]
5155
====
@@ -75,6 +79,15 @@ include::{sourcedir}/_0199_BinaryTreeRightSideView_1.java[tag=answer]
7579
include::{sourcedir}/_0199_BinaryTreeRightSideView_2.java[tag=answer]
7680
----
7781
--
82+
83+
三刷::
84+
+
85+
--
86+
[{java_src_attr}]
87+
----
88+
include::{sourcedir}/_0199_BinaryTreeRightSideView_3.java[tag=answer]
89+
----
90+
--
7891
====
7992

8093
这道题是 xref:0102-binary-tree-level-order-traversal.adoc[102. Binary Tree Level Order Traversal] 的延伸。

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,11 @@ endif::[]
17841784
|{doc_base_url}/0230-kth-smallest-element-in-a-bst.adoc[题解]
17851785
|✅ DFS + 树。递归方式OK,迭代方式还要再体会一下。
17861786

1787+
|{counter:codes2503}
1788+
|{leetcode_base_url}/binary-tree-right-side-view/[199. 二叉树的右视图^]
1789+
|{doc_base_url}/0199-binary-tree-right-side-view.adoc[题解]
1790+
|✅ 深度优先遍历。可以不用 `Map`,直接用 `List` 即可。要注意**先根遍历****中根遍历**的区别:先根遍历每层都是按顺序从上到下添加到 `List`;而中根遍历,每次都是最下层到,所以,要添加 `null` 占位符把 `List` 给撑起来,后续直接按照坐标设置。
1791+
17871792
|===
17881793

17891794
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
import com.diguage.util.TreeNodes;
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
public class _0199_BinaryTreeRightSideView_3 {
11+
// tag::answer[]
12+
13+
/**
14+
* @author D瓜哥 · https://www.diguage.com
15+
* @since 2025-10-25 10:51:00
16+
*/
17+
public List<Integer> rightSideView(TreeNode root) {
18+
List<Integer> result = new ArrayList<>();
19+
dfs(root, result, 0);
20+
return result;
21+
}
22+
23+
private void dfs(TreeNode root, List<Integer> result, int level) {
24+
if (null == root) {
25+
return;
26+
}
27+
// 如果是先根遍历,每层都会根节点先到,
28+
// 直接在 List 里面追加(add)即可
29+
if (level == result.size()) {
30+
result.add(root.val);
31+
} else {
32+
// 如果是左中右遍历,左中节点先到,那么就需要更新
33+
// 如果是右中左遍历,右节点先到,直接添加就无需更新
34+
result.set(level, root.val);
35+
}
36+
dfs(root.left, result, level + 1);
37+
// 如果是中根遍历,会先遍历到最深的叶子节点,
38+
// 所以需要用 null 占位符把 List 给撑起来
39+
// while (result.size() <= level) {
40+
// result.add(null);
41+
// }
42+
// result.set(level, root.val);
43+
dfs(root.right, result, level + 1);
44+
}
45+
// end::answer[]
46+
47+
static void main() {
48+
new _0199_BinaryTreeRightSideView_3()
49+
.rightSideView(TreeNodes.buildTree(Arrays.asList(1, 2, 3, 4, null, null, null, 5)));
50+
}
51+
52+
}

0 commit comments

Comments
 (0)