-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum Value.java
46 lines (39 loc) · 1.14 KB
/
Maximum Value.java
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
// Date : 02.01.2023
// problem statement: Maximum Value(Mid Level)
// Time complexity : O(N)
// Space complexity : O(N)
class Solution {
ArrayList<Integer> maximumValue(Node node) {
//code here
// using a queue datastrucutre
ArrayDeque<Node> dq = new ArrayDeque<>();
//adding elements
dq.add(node);
//creating a arraylist for storing the elements and return it
ArrayList <Integer> list = new ArrayList<>();
while(dq.size() > 0)
{
int n = dq.size();
int max = 0;
//decrementing the queue till 0
while(n --> 0)
{
//head node
Node p = dq.pop();
max = Math.max(max,p.data);
//for the left part of the tree
if(p.left != null)
{
dq.add(p.left);
}
//for the right part
if(p.right != null)
{
dq.add(p.right);
}
}
list.add(max);
}
return list;
}
}