diff --git a/src/main/java/com/thealgorithms/datastructures/BinaryTreeRightSideView.java b/src/main/java/com/thealgorithms/datastructures/BinaryTreeRightSideView.java new file mode 100644 index 000000000000..d92bab6d4d31 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/BinaryTreeRightSideView.java @@ -0,0 +1,70 @@ +package com.thealgorithms.datastructures; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + + +public class BinaryTreeRightSideView { + + + public static class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int val) { + this.val = val; + } + } + + public List rightSideView(TreeNode root) { + List result = new ArrayList<>(); + if (root == null) { + return result; + } + + Queue queue = new LinkedList<>(); + queue.add(root); + + while (!queue.isEmpty()) { + + int levelSize = queue.size(); + + for (int i = 0; i < levelSize; i++) { + TreeNode currentNode = queue.poll(); + + + if (i == levelSize - 1) { + result.add(currentNode.val); + } + + + if (currentNode.left != null) { + queue.add(currentNode.left); + } + if (currentNode.right != null) { + queue.add(currentNode.right); + } + } + } + return result; + } + + + public static void main(String[] args) { + + BinaryTreeRightSideView solution = new BinaryTreeRightSideView(); + + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + root.left.right = new TreeNode(5); + root.right.right = new TreeNode(4); + + List view = solution.rightSideView(root); + + System.out.println("Right Side View: " + view); + } +} \ No newline at end of file