|
| 1 | +package com.thealgorithms.datastructures.trees; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Queue; |
| 7 | + |
| 8 | +/** |
| 9 | + * The Right View of a Binary Tree is the set of nodes visible when the tree |
| 10 | + * is viewed from the right side. For each level, the rightmost node is part |
| 11 | + * of the right view. |
| 12 | + * |
| 13 | + * <p>This implementation provides both DFS and BFS approaches.</p> |
| 14 | + */ |
| 15 | +public class RightViewOfBinaryTree { |
| 16 | + |
| 17 | + /** |
| 18 | + * Node class structure. If the repository already has a standard Node class, |
| 19 | + * reuse it instead of redefining this one. |
| 20 | + */ |
| 21 | + static class Node { |
| 22 | + int data; |
| 23 | + Node left; |
| 24 | + Node right; |
| 25 | + |
| 26 | + Node(int data) { |
| 27 | + this.data = data; |
| 28 | + left = right = null; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns the right view of the binary tree using DFS. |
| 34 | + * |
| 35 | + * @param root the root of the binary tree |
| 36 | + * @return list of node values visible from the right |
| 37 | + */ |
| 38 | + public static List<Integer> rightViewDFS(Node root) { |
| 39 | + List<Integer> result = new ArrayList<>(); |
| 40 | + dfsHelper(root, 0, result); |
| 41 | + return result; |
| 42 | + } |
| 43 | + |
| 44 | + private static void dfsHelper(Node node, int level, List<Integer> result) { |
| 45 | + if (node == null) return; |
| 46 | + if (level == result.size()) { |
| 47 | + result.add(node.data); |
| 48 | + } |
| 49 | + dfsHelper(node.right, level + 1, result); |
| 50 | + dfsHelper(node.left, level + 1, result); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns the right view using a level-order (BFS) approach. |
| 55 | + * |
| 56 | + * @param root the root node |
| 57 | + * @return list of right view nodes |
| 58 | + */ |
| 59 | + public static List<Integer> rightViewBFS(Node root) { |
| 60 | + List<Integer> result = new ArrayList<>(); |
| 61 | + if (root == null) return result; |
| 62 | + |
| 63 | + Queue<Node> queue = new LinkedList<>(); |
| 64 | + queue.offer(root); |
| 65 | + |
| 66 | + while (!queue.isEmpty()) { |
| 67 | + int size = queue.size(); |
| 68 | + for (int i = 0; i < size; i++) { |
| 69 | + Node current = queue.poll(); |
| 70 | + if (i == size - 1) { |
| 71 | + result.add(current.data); |
| 72 | + } |
| 73 | + if (current.left != null) queue.offer(current.left); |
| 74 | + if (current.right != null) queue.offer(current.right); |
| 75 | + } |
| 76 | + } |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + // Example usage |
| 81 | + public static void main(String[] args) { |
| 82 | + Node root = new Node(1); |
| 83 | + root.left = new Node(2); |
| 84 | + root.right = new Node(3); |
| 85 | + root.left.left = new Node(4); |
| 86 | + root.left.right = new Node(5); |
| 87 | + root.right.right = new Node(6); |
| 88 | + |
| 89 | + System.out.println("Right View (DFS): " + rightViewDFS(root)); |
| 90 | + System.out.println("Right View (BFS): " + rightViewBFS(root)); |
| 91 | + } |
| 92 | +} |
0 commit comments