Skip to content

Commit 46ea8e8

Browse files
chore: Merge pull request #197 from saulmaldonado/feat/n-ary-tree-preorder-traversal
n ary tree preorder traversal
2 parents 2f057e5 + 85298bc commit 46ea8e8

8 files changed

+164
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# N-ary Tree Preorder Traversal
2+
3+
## Difficulty
4+
5+
![Easy](https://img.shields.io/badge/easy-5cb85c?style=for-the-badge&logoColor=white)
6+
7+
## Problem
8+
9+
Given the root of an n-ary tree, return the preorder traversal of its nodes' values.
10+
11+
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
12+
13+
### Example 1
14+
15+
![Example 1](./images/example-1.png)
16+
17+
```
18+
Input: root = [1,null,3,2,4,null,5,6]
19+
Output: [1,3,5,6,2,4]
20+
```
21+
22+
### Example 2
23+
24+
![Example 2](./images/example-2.png)
25+
26+
```
27+
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
28+
Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
29+
```
30+
31+
### Constraints
32+
33+
`The number of nodes in the tree is in the range [0, 104].`
34+
35+
`0 <= Node.val <= 104`
36+
37+
`The height of the n-ary tree is less than or equal to 1000.`
38+
39+
<details>
40+
<summary>Solutions (Click to expand)</summary>
41+
42+
### Explanation
43+
44+
#### Solution
45+
46+
##### Intuition
47+
48+
A Preorder traversal of a Binary Tree involves traversing the tree in a root, left child, right child order. We can apply this to an N-ary tree by traversing the root and all its children in order from left to right.
49+
50+
##### Implementation
51+
52+
We will do a normal DFS traversal of the tree where every node we visit will first get inserted into a list. All of the children in the `List<Node>` will be traversed from left to right.
53+
54+
![Solution 1](./images/solution-1.png)
55+
56+
- [JavaScript](./n-ary-tree-preorder-traversal.js)
57+
- [TypeScript](./n-ary-tree-preorder-traversal.ts)
58+
- [Java](./n-ary-tree-preorder-traversal.java)
59+
- [Go](./n-ary-tree-preorder-traversal.go)
60+
61+
</details>
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package narytreepreordertraversal
2+
3+
type Node struct {
4+
Val int
5+
Children []*Node
6+
}
7+
8+
func preorder(root *Node) []int {
9+
list := new([]int)
10+
traverse(root, list)
11+
return *list
12+
}
13+
14+
func traverse(root *Node, list *[]int) {
15+
if root == nil {
16+
return
17+
}
18+
19+
*list = append(*list, root.Val)
20+
21+
for _, node := range root.Children {
22+
traverse(node, list)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public List<Integer> preorder(Node root) {
3+
List<Integer> list = new ArrayList<>();
4+
5+
traverse(root, list);
6+
7+
return list;
8+
}
9+
10+
private void traverse(Node root, List<Integer> list) {
11+
if (root == null) {
12+
return;
13+
}
14+
15+
list.add(root.val);
16+
17+
for (Node node : root.children) {
18+
traverse(node, list);
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function Node(val, children) {
2+
this.val = val;
3+
this.children = children;
4+
}
5+
6+
/**
7+
* @param {Node} root
8+
* @return {number[]}
9+
*/
10+
function preorder(root) {
11+
const list = [];
12+
traverse(root, list);
13+
return list;
14+
}
15+
16+
/**
17+
*
18+
* @param {Node} root
19+
* @param {number[]} list
20+
* @returns
21+
*/
22+
function traverse(root, list) {
23+
if (root === null) {
24+
return;
25+
}
26+
27+
list.push(root.val);
28+
29+
for (let node of root.children) {
30+
traverse(node, list);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class NAryNode {
2+
val: number;
3+
children: NAryNode[];
4+
constructor(val?: number) {
5+
this.val = val === undefined ? 0 : val;
6+
this.children = [];
7+
}
8+
}
9+
10+
function preorder(root: NAryNode | null): number[] {
11+
const list: number[] = [];
12+
traverse(root, list);
13+
return list;
14+
}
15+
16+
function traverse(root: NAryNode | null, list: number[]) {
17+
if (root === null) {
18+
return;
19+
}
20+
21+
list.push(root.val);
22+
23+
for (let node of root.children) {
24+
traverse(node, list);
25+
}
26+
}

0 commit comments

Comments
 (0)