-
Notifications
You must be signed in to change notification settings - Fork 0
/
p160.java
75 lines (61 loc) · 1.56 KB
/
p160.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
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
// https://practice.geeksforgeeks.org/problems/reverse-level-order-traversal/1
/*Given a binary tree of size N, find its reverse level order traversal. ie- the traversal must begin from the last level.
Example 1:
Input :
1
/ \
3 2
Output: 3 2 1
Explanation:
Traversing level 1 : 3 2
Traversing level 0 : 1
Example 2:
Input :
10
/ \
20 30
/ \
40 60
Output: 40 60 20 30 10
Explanation:
Traversing level 2 : 40 60
Traversing level 1 : 20 30
Traversing level 0 : 10
Your Task:
You dont need to read input or print anything. Complete the function reverseLevelOrder() which takes the root of the tree as input parameter and returns a list containing the reverse level order traversal of the given tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^4*/
/* class Node
{
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}*/
class Tree{
public ArrayList<Integer> reverseLevelOrder(Node node) {
ArrayList<Integer> ls=new ArrayList<>();
Stack<Integer> st=new Stack<>();
Queue<Node> q=new LinkedList<>();
q.add(node);
while(!q.isEmpty()){
Node curr=q.remove();
st.push(curr.data);
if(curr.right!=null){
q.add(curr.right);
}
if(curr.left!=null){
q.add(curr.left);
}
}
while(!st.isEmpty()){
ls.add(st.pop());
}
return ls;
}
}