generated from AkankshaAI/Hacktoberfest2023-Excluded
-
Notifications
You must be signed in to change notification settings - Fork 673
/
Copy pathBinaryTree_Infect_time.js
126 lines (101 loc) · 2.32 KB
/
BinaryTree_Infect_time.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// JS code for Minimum time required to infect all the nodes of Binary tree
class Node
{
constructor(item)
{
this.item = item;
this.left = this.right = null;
}
}
var node=null;
var item=null;
function findParent(root, p, parent,start)
{
if (root == null)
return;
// Store parent of current node
parent[root.item] = p;
if (root.item == start) {
node = root;
}
findParent(root.left, root, parent, start);
findParent(root.right, root, parent, start);
}
function amountOfTime(root,start)
{
let parent = new Array(100005);
parent.fill(null);
findParent(root, null, parent, start);
let visited=new Array(100005);
visited.fill(false);
let q=[];
// Push special tree node into the
// queue and make it visited.
q.push(node);
visited[start] = true;
// This store the minimum time require
// to infect all the tree node.
let result = -1;
while (q.length > 0) {
let n = q.length;
for (let i = 0; i < n; i++) {
let curr = q[0];
let currNode = curr.item;
q.shift();
// Check if parent of currNode
// exist and not visited yet
// then push this parent of
// current node into queue.
if (parent[currNode] != null
&& visited[parent[currNode].item]
== false) {
visited[parent[currNode].item] = true;
q.push(parent[currNode]);
}
// Check if current node left
// child exist and not
// visited yet.
if (curr.left
&& visited[curr.left.item] == false) {
visited[curr.left.item] = true;
q.push(curr.left);
}
// Check if current node right
// child exist and not
// visited yet.
if (curr.right
&& visited[curr.right.item] == false) {
visited[curr.right.item] = true;
q.push(curr.right);
}
}
// Increment the time
result++;
}
// Return the result.
return result;
}
// Driver Code
/* 10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Let us create Binary Tree as shown
above */
let root = new Node(10);
root.left = new Node(12);
root.right = new Node(13);
root.right.left = new Node(14);
root.right.right = new Node(15);
root.right.left.left = new Node(21);
root.right.left.right = new Node(22);
root.right.right.left = new Node(23);
root.right.right.right = new Node(24);
let start = 14;
// Function call
let result = amountOfTime(root, start);
console.log(result);
// This code is contributed by SumitAwate