-
Notifications
You must be signed in to change notification settings - Fork 0
/
p162.java
71 lines (60 loc) · 1.4 KB
/
p162.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
// https://practice.geeksforgeeks.org/problems/right-view-of-binary-tree/1
/*Given a Binary Tree, find Right view of it. Right view of a Binary Tree is set of nodes visible when tree is viewed from right side.
Right view of following tree is 1 3 7 8.
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Example 1:
Input:
1
/ \
3 2
Output: 1 2
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 10 30 60
Your Task:
Just complete the function rightView() that takes node as parameter and returns the right view as a list.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 ≤ Number of nodes ≤ 105
0 ≤ Data of a node ≤ 105*/
/*Complete The Function Provided
Given Below is The Node Of Tree
class Node
{
int data;
Node left, right;
public Node(int data)
{
this.data = data;
left = right = null;
}
}*/
class Solution{
ArrayList<Integer> rightView(Node node) {
ArrayList<Integer> ls=new ArrayList<>();
rightRecur(node,0,ls);
return ls;
}
public static void rightRecur(Node node,int level,List<Integer> ls){
if(node==null){
return;
}
if(ls.size()==level){
ls.add(node.data);
}
rightRecur(node.right,level+1,ls);
rightRecur(node.left,level+1,ls);
}
}