Skip to content

Commit 8adf833

Browse files
committed
Binary Tree Level Order Traversal & Fibonacci Number
1 parent eacb20a commit 8adf833

File tree

10 files changed

+97
-1
lines changed

10 files changed

+97
-1
lines changed

README.MD

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ Solving problems with LeetCode
3030
| [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 |
3131
| [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findFirstBadVersion/findFirstBadVersion.js) | Easy |
3232
| [Binary Search](https://leetcode.com/problems/binary-search/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/binarySearch/binarySearch.js) | Easy |
33-
| [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 |
33+
| [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/fib/fib.js) | Easy |
34+
| [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/detectCycle/detectCycle.js) | Medium |
35+
| [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/levelOrder/levelOrder.js) | Medium |

helpers/treeNode.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function TreeNode(val, left, right) {
2+
this.val = (val === undefined ? 0 : val);
3+
this.left = (left === undefined ? null : left);
4+
this.right = (right === undefined ? null : right);
5+
}
6+
7+
module.exports = TreeNode;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"type": "module",
23
"devDependencies": {
34
"jest": "^29.3.1"
45
},

solutions/fib/README.MD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## [509. Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)
2+
3+
The Fibonacci numbers, commonly denoted `F(n)` form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from `0` and `1`. That is,
4+
5+
```
6+
F(0) = 0, F(1) = 1
7+
F(n) = F(n - 1) + F(n - 2), for n > 1.
8+
```
9+
10+
Given `n`, calculate `F(n)`.

solutions/fib/fib.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
function fib(n) {
6+
if (n === 0) return 0;
7+
if (n === 1) return 1;
8+
9+
return fib(n - 1) + fib(n - 2);
10+
}
11+
12+
module.exports = fib;

solutions/fib/fib.spec.js

Whitespace-only changes.

solutions/index.d.ts

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

solutions/levelOrder/README.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [102. Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)
2+
3+
Given the `root` of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

solutions/levelOrder/levelOrder.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {TreeNode} root
3+
* @return {number[][]}
4+
*/
5+
function levelOrder(root) {
6+
if (!root) return [];
7+
8+
const result = [],
9+
queue = [root];
10+
11+
while (queue.length > 0) {
12+
const level = [];
13+
const size = queue.length;
14+
15+
for (let i = 0; i < size; i++) {
16+
const node = queue.shift();
17+
level.push(node.val);
18+
if (node.left) queue.push(node.left);
19+
if (node.right) queue.push(node.right);
20+
}
21+
22+
result.push(level);
23+
}
24+
25+
return result;
26+
}
27+
28+
module.exports = levelOrder;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const TreeNode = require('../../helpers/treeNode');
2+
const levelOrder = require('./levelOrder');
3+
4+
describe('Binary Tree Level Order Traversal', () => {
5+
test('returns an empty array for empty input', () => {
6+
const root = null;
7+
const expected = [];
8+
expect(levelOrder(root)).toEqual(expected);
9+
});
10+
11+
test('returns the correct result for a tree with one node', () => {
12+
const root = new TreeNode(1);
13+
const expected = [[1]];
14+
expect(levelOrder(root)).toEqual(expected);
15+
});
16+
17+
test('returns the correct result for a tree with multiple levels', () => {
18+
const root = new TreeNode(3,
19+
new TreeNode(9),
20+
new TreeNode(20,
21+
new TreeNode(15),
22+
new TreeNode(7),
23+
),
24+
);
25+
const expected = [[3], [9, 20], [15, 7]];
26+
expect(levelOrder(root)).toEqual(expected);
27+
});
28+
});

0 commit comments

Comments
 (0)