-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMirrorOfATree.js
60 lines (54 loc) · 1.4 KB
/
MirrorOfATree.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @author Anirudh Sharma
*
* Given a binary tree, the task is to create a new binary tree which is a mirror image of
* the given binary tree.
*/
const mirror = (root) => {
// Base case
if (root === null) {
return null;
}
// Get left and right children of the node
let left = mirror(root.left);
let right = mirror(root.right);
// Swap the nodes
root.right = left;
root.left = right;
return root;
};
const inorderTraversal = (node) => {
if (node === null) {
return;
}
inorderTraversal(node.left);
console.log(node.data + " ");
inorderTraversal(node.right);
};
function Node(data, left, right) {
this.data = (data === undefined ? 0 : data);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
const main = () => {
let root = new Node(1);
root.left = new Node(3);
root.right = new Node(2);
inorderTraversal(mirror(root));
console.log();
root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(60);
inorderTraversal(mirror(root));
console.log();
root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
inorderTraversal(mirror(root));
console.log();
};
main();