Skip to content

Commit aaf526d

Browse files
committed
Solve problem: N-ary Tree Preorder Traversal
1 parent ed6c2b9 commit aaf526d

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ Solving problems with LeetCode
2727
| [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isIsomorphic/isIsomorphic.js) | Easy |
2828
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/mergeTwoLists/mergeTwoLists.js) | Easy |
2929
| [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/longestPalindrome/longestPalindrome.js) | Easy |
30+
| [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/preorder/preorder.js) | Easy |
3031
| [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/description/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/detectCycle/detectCycle.js) | Medium |

solutions/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ declare class ListNode {
33
next: ListNode | null;
44
constructor(val: number);
55
}
6+
// @ts-ignore
7+
declare class Node {
8+
val: number;
9+
children: Node[];
10+
constructor(val: number, children: Node[]);
11+
}

solutions/preorder/README.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [589. N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)
2+
3+
Given the `root` of an n-ary tree, return the preorder traversal of its nodes' values.
4+
5+
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

solutions/preorder/preorder.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {Node|null} root
3+
* @return {number[]}
4+
*/
5+
function preorder(root) {
6+
if (!root) return [];
7+
8+
const stack = [root];
9+
const result = [];
10+
11+
while (stack.length > 0) {
12+
const node = stack.pop();
13+
result.push(node.val);
14+
for (let i = node.children.length - 1; i >= 0; i--) {
15+
stack.push(node.children[i]);
16+
}
17+
}
18+
19+
return result;
20+
}
21+
22+
// function preorder(root) {
23+
// if (!root) return []
24+
//
25+
// const queue = root.children;
26+
// const result = [root.val];
27+
//
28+
// while (queue.length > 0) {
29+
// const node = queue.shift();
30+
// result.push(node.val);
31+
//
32+
// if (node.children.length > 0) {
33+
// queue.unshift(...node.children);
34+
// }
35+
// }
36+
//
37+
// return result;
38+
// }
39+
40+
module.exports = preorder;

solutions/preorder/preorder.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const preorder = require('./preorder');
2+
3+
class Node {
4+
val;
5+
children;
6+
7+
constructor(val, children) {
8+
this.val = val;
9+
this.children = children;
10+
}
11+
}
12+
13+
function createTreeRoot() {
14+
return new Node(1, [
15+
new Node(2, [
16+
new Node(4, []),
17+
new Node(5, [])
18+
]),
19+
new Node(3, [
20+
new Node(6, []),
21+
new Node(7, [
22+
new Node(8, [])
23+
])
24+
])
25+
]);
26+
}
27+
28+
describe('N-ary Tree Preorder Traversal', () => {
29+
test('should return an empty array when root is null', () => {
30+
expect(preorder(null)).toEqual([]);
31+
});
32+
33+
test('should return an array of values in preorder traversal order', () => {
34+
const root = createTreeRoot();
35+
expect(preorder(root)).toEqual([1, 2, 4, 5, 3, 6, 7, 8]);
36+
});
37+
38+
test('should return an array with a single value when root has no children', () => {
39+
const root = new Node(1, []);
40+
expect(preorder(root)).toEqual([1]);
41+
});
42+
});

0 commit comments

Comments
 (0)