-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvert_bst.js
executable file
·69 lines (59 loc) · 1.58 KB
/
invert_bst.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
61
62
63
64
65
66
67
68
69
var invertTree = function(root) {
if(root==null){
return root;
}
if(root.left == null && root.right == null){
return root;
}
if(root.left != null) invertTree(root.left) ;
if(root.right != null) invertTree(root.right) ;
let temp = root.left;
root.left = root.right;
root.right = temp;
return root;
};
// ---just to execute --
function TreeNode(val, left, right) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
let root = [4,2,7,1,3,6,9]
let tree = createTree(root)
console.log(printTree(tree, []));
let invertedTree = invertTree(tree);
console.log(printTree(invertedTree, []));
function createTree(array){
let head = new TreeNode(array[0], null, null);
array = array.slice(1, array.length)
for(let num of array){
insertIntoTree(head, num);
}
return head;
}
function insertIntoTree(tree, number){
if(number < tree.val) {
if(tree.left == null) {
tree.left = new TreeNode(number, null, null);
return;
}
insertIntoTree(tree.left, number)
}
else if(number > tree.val){
if(tree.right == null){
tree.right = new TreeNode(number,null,null);
return;
}
insertIntoTree(tree.right, number);
}
}
function printTree(root, print){
print.push(root.val)
if(root.left != null){
printTree(root.left, print)
}
if(root.right != null){
printTree(root.right, print)
}
return print
}