Skip to content

Commit c202f59

Browse files
committed
Added java code
1 parent 7530882 commit c202f59

1 file changed

Lines changed: 48 additions & 43 deletions

File tree

src/main/java/com/thealgorithms/datastructures/BinaryTreeRightSideView.java

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,66 @@
55
import java.util.List;
66
import java.util.Queue;
77

8+
89
public class BinaryTreeRightSideView {
910

10-
// FIX: Make the TreeNode class static.
11-
static class TreeNode {
11+
12+
public static class TreeNode {
1213
int val;
13-
TreeNode left, right;
14-
TreeNode(int x) {
15-
this.val = x;
14+
TreeNode left;
15+
TreeNode right;
16+
17+
TreeNode(int val) {
18+
this.val = val;
1619
}
1720
}
1821

19-
// FIX: Make the RightSideView class static.
20-
public static class RightSideView {
21-
public List<Integer> rightSideView(TreeNode root) {
22-
List<Integer> result = new ArrayList<>();
23-
if (root == null) {
24-
return result;
25-
}
22+
public List<Integer> rightSideView(TreeNode root) {
23+
List<Integer> result = new ArrayList<>();
24+
if (root == null) {
25+
return result;
26+
}
27+
28+
Queue<TreeNode> queue = new LinkedList<>();
29+
queue.add(root);
30+
31+
while (!queue.isEmpty()) {
32+
33+
int levelSize = queue.size();
2634

27-
Queue<TreeNode> queue = new LinkedList<>();
28-
queue.add(root);
29-
30-
while (!queue.isEmpty()) {
31-
int size = queue.size();
32-
for (int i = 0; i < size; i++) {
33-
TreeNode node = queue.poll();
34-
35-
// If it's the last node in this level, add it to the result.
36-
if (i == size - 1) {
37-
result.add(node.val);
38-
}
39-
40-
if (node.left != null) {
41-
queue.add(node.left);
42-
}
43-
if (node.right != null) {
44-
queue.add(node.right);
45-
}
35+
for (int i = 0; i < levelSize; i++) {
36+
TreeNode currentNode = queue.poll();
37+
38+
39+
if (i == levelSize - 1) {
40+
result.add(currentNode.val);
41+
}
42+
43+
44+
if (currentNode.left != null) {
45+
queue.add(currentNode.left);
46+
}
47+
if (currentNode.right != null) {
48+
queue.add(currentNode.right);
4649
}
4750
}
48-
return result;
4951
}
52+
return result;
53+
}
5054

51-
// The main method can now correctly instantiate these static classes.
52-
public static void main(String[] args) {
53-
RightSideView sol = new RightSideView();
55+
56+
public static void main(String[] args) {
57+
58+
BinaryTreeRightSideView solution = new BinaryTreeRightSideView();
5459

55-
TreeNode root = new TreeNode(1);
56-
root.left = new TreeNode(2);
57-
root.right = new TreeNode(3);
58-
root.left.right = new TreeNode(5);
59-
root.right.right = new TreeNode(4);
60+
TreeNode root = new TreeNode(1);
61+
root.left = new TreeNode(2);
62+
root.right = new TreeNode(3);
63+
root.left.right = new TreeNode(5);
64+
root.right.right = new TreeNode(4);
6065

61-
// The logic correctly produces the right side view.
62-
System.out.println(sol.rightSideView(root)); // Expected Output: [1, 3, 4]
63-
}
66+
List<Integer> view = solution.rightSideView(root);
67+
68+
System.out.println("Right Side View: " + view);
6469
}
6570
}

0 commit comments

Comments
 (0)