Skip to content

Commit 5d93c0f

Browse files
committed
added test suite for Question 226
1 parent ee3bc23 commit 5d93c0f

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

javascript/questions-200-300/question-226/question-226.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ const invertTree = function(root) {
3131
}
3232
return root;
3333
};
34+
35+
module.exports = invertTree;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const invertTree = require(`./question-226`);
2+
const TreeNode = require(`../../util/TreeNode`);
3+
4+
test(`expects the result of inverting a binary tree [4, 2, 7, 1, 3, 6, 9] to be [4, 7, 2, 9, 6, 3, 1]`, () => {
5+
const t7 = TreeNode(9);
6+
const t6 = TreeNode(6);
7+
const t5 = TreeNode(3);
8+
const t4 = TreeNode(1);
9+
const t3 = TreeNode(7, t6, t7);
10+
const t2 = TreeNode(2, t4, t5);
11+
const t1 = TreeNode(4, t2, t3);
12+
13+
const t3inverted = TreeNode(7, t7, t6);
14+
const t2inverted = TreeNode(2, t5, t4);
15+
const t1inverted = TreeNode(4, t3inverted, t2inverted);
16+
17+
expect(invertTree(t1)).toStrictEqual(t1inverted);
18+
});
19+
20+
test(`expects the result of inverting a binary tree [2, 1, 3] to be [2, 3, 1]`, () => {
21+
const t3 = TreeNode(3);
22+
const t2 = TreeNode(1);
23+
const t1 = TreeNode(2, t2, t3);
24+
25+
const t1inverted = TreeNode(2, t3, t2);
26+
27+
expect(invertTree(t1)).toStrictEqual(t1inverted);
28+
});
29+
30+
test(`expects the result of inverting a binary tree [] to be []`, () => {
31+
const t1 = TreeNode();
32+
const t1inverted = TreeNode();
33+
34+
expect(invertTree(t1)).toStrictEqual(t1inverted);
35+
});

javascript/util/TreeNode.js

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

0 commit comments

Comments
 (0)