-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRight_view.java
More file actions
86 lines (71 loc) · 1.67 KB
/
Right_view.java
File metadata and controls
86 lines (71 loc) · 1.67 KB
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
// JAVA program to print right view of
// Binary Tree
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
// A Binary Tree Node
//class Node {
// int data;
// Node left, right;
// public Node(int d)
// {
// data = d;
// left = right = null;
// }
//}
class Right_view {
Node root;
// function to print Right view of
// binary tree
void rightView(Node root)
{
if (root == null) {
return;
}
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
// get number of nodes for each level
int n = q.size();
// traverse all the nodes of the current level
for (int i = 0; i < n; i++) {
Node curr = q.peek();
q.remove();
// print the last node of each level
if (i == n - 1) {
System.out.print(curr.data);
System.out.print(" ");
}
// if left child is not null add it into
// the
// queue
if (curr.left != null) {
q.add(curr.left);
}
// if right child is not null add it into
// the
// queue
if (curr.right != null) {
q.add(curr.right);
}
}
}
}
// Driver code
public static void main(String[] args)
{
// Let's construct the tree as
// shown in example
Right_view tree = new Right_view();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
tree.root.right.left.right = new Node(8);
tree.rightView(tree.root);
}
}
// This code is contributed by Biswajit Rajak