• 문제 링크
N-ary Tree Preorder Traversal - LeetCode
Can you solve this real interview question? N-ary Tree Preorder Traversal - Given the root of an n-ary tree, return the preorder traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversal. Each group of chil
leetcode.com
• 풀이 코드
import java.util.ArrayList;
import java.util.List;
public class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> preorder(Node root) {
dfs(root);
return list;
}
private void dfs(Node node) {
if (node == null) return;
list.add(node.val);
for (Node child : node.children) dfs(child);
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 594. Longest Harmonious Subsequence - Java (0) | 2026.09.08 |
|---|---|
| [LeetCode] 590. N-ary Tree Postorder Traversal - Java (0) | 2026.09.07 |
| [LeetCode] 575. Distribute Candies - Java (0) | 2026.09.05 |
| [LeetCode] 572. Subtree of Another Tree - Java (0) | 2026.09.04 |
| [LeetCode] 566. Reshape the Matrix - Java (0) | 2026.09.03 |
댓글