-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path226. Invert Binary Tree.js
45 lines (36 loc) · 1.1 KB
/
226. Invert Binary Tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { toOrderedTree, treeToArray } from "./Utils";
/**
* Given the root of a binary tree, invert the tree, and return its root.
*/
class Test {
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.value = (value===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
invertTree(root) {
if (root.left || root.right) {
// go deep
const newRight = this.invertTree(root.left);
const newLeft = this.invertTree(root.right)
//swap
root.left = newLeft;
root.right = newRight;
}
return root;
}
}
const testExecutor = new Test();
const testCase = [
{par1: [4,2,7,1,3,6,9], result: [4,7,2,9,6,3,1]},
{par1: [2,1,3], result: [2,3,1]},
];
testCase.forEach(testCase => {
console.log('Executing test', testCase);
const result = testExecutor.invertTree(toOrderedTree(testCase.par1));
console.log('Result:', treeToArray(result));
console.assert(treeToArray(result).every((el, i) => testCase.result[i] === el));
})