|
| 1 | +--- |
| 2 | +id: binary-tree-right-side-view |
| 3 | +title: Binary Tree Right Side View Solution |
| 4 | +sidebar_label: 0199 Binary Tree Right Side View |
| 5 | +tags: |
| 6 | + - Binary Tree |
| 7 | + - Depth-First Search |
| 8 | + - Breadth-First Search |
| 9 | + - LeetCode |
| 10 | + - Java |
| 11 | + - Python |
| 12 | + - C++ |
| 13 | +description: "This is a solution to the Binary Tree Right Side View problem on LeetCode." |
| 14 | +--- |
| 15 | + |
| 16 | +## Problem Description |
| 17 | + |
| 18 | +Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +``` |
| 25 | +Input: root = [1,2,3,null,5,null,4] |
| 26 | +Output: [1,3,4] |
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +``` |
| 32 | +Input: root = [1,null,3] |
| 33 | +Output: [1,3] |
| 34 | +``` |
| 35 | + |
| 36 | +**Example 3:** |
| 37 | + |
| 38 | +``` |
| 39 | +Input: root = [] |
| 40 | +Output: [] |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +### Constraints |
| 45 | + |
| 46 | +- The number of nodes in the tree is in the range `[0, 100]`. |
| 47 | +- $-100 <=$ Node.val $<= 100$ |
| 48 | + |
| 49 | +## Solution for Binary Tree Right Side View Problem |
| 50 | + |
| 51 | +### Intuition |
| 52 | + |
| 53 | +The intuition behind the solution is to perform a level-order traversal (BFS) of the binary tree and record the value of the last node at each level, which represents the view from the right side. |
| 54 | + |
| 55 | +### Approach |
| 56 | + |
| 57 | +1. Initialize an empty result list to store the right side view of the tree. |
| 58 | +2. Perform a level-order traversal (BFS) of the tree. |
| 59 | +3. At each level, record the value of the last node in the result list. |
| 60 | +4. Return the result list. |
| 61 | + |
| 62 | +#### Code in Different Languages |
| 63 | + |
| 64 | +<Tabs> |
| 65 | + <TabItem value="Python" label="Python" default> |
| 66 | + <SolutionAuthor name="@mahek0620"/> |
| 67 | + ```python |
| 68 | + |
| 69 | + from collections import deque |
| 70 | + class TreeNode(object): |
| 71 | + def __init__(self, val=0, left=None, right=None): |
| 72 | + self.val = val |
| 73 | + self.left = left |
| 74 | + self.right = right |
| 75 | + |
| 76 | + class Solution(object): |
| 77 | + def rightSideView(self, root): |
| 78 | + result = [] |
| 79 | + if not root: |
| 80 | + return result |
| 81 | + |
| 82 | + queue = deque([root]) |
| 83 | + |
| 84 | + while queue: |
| 85 | + level_length = len(queue) |
| 86 | + for i in range(level_length): |
| 87 | + node = queue.popleft() |
| 88 | + if i == level_length - 1: |
| 89 | + result.append(node.val) |
| 90 | + if node.left: |
| 91 | + queue.append(node.left) |
| 92 | + if node.right: |
| 93 | + queue.append(node.right) |
| 94 | + |
| 95 | + return result |
| 96 | + ``` |
| 97 | + </TabItem> |
| 98 | + <TabItem value="Java" label="Java"> |
| 99 | + <SolutionAuthor name="@mahek0620"/> |
| 100 | + ```java |
| 101 | + |
| 102 | + import java.util.*; |
| 103 | + |
| 104 | + class TreeNode { |
| 105 | + int val; |
| 106 | + TreeNode left; |
| 107 | + TreeNode right; |
| 108 | + TreeNode() {} |
| 109 | + TreeNode(int val) { this.val = val; } |
| 110 | + TreeNode(int val, TreeNode left, TreeNode right) { |
| 111 | + this.val = val; |
| 112 | + this.left = left; |
| 113 | + this.right = right; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + class Solution { |
| 118 | + public List<Integer> rightSideView(TreeNode root) { |
| 119 | + List<Integer> result = new ArrayList<>(); |
| 120 | + if (root == null) return result; |
| 121 | + |
| 122 | + Queue<TreeNode> queue = new LinkedList<>(); |
| 123 | + queue.add(root); |
| 124 | + |
| 125 | + while (!queue.isEmpty()) { |
| 126 | + int size = queue.size(); |
| 127 | + for (int i = 0; i < size; i++) { |
| 128 | + TreeNode node = queue.poll(); |
| 129 | + if (i == size - 1) result.add(node.val); |
| 130 | + if (node.left != null) queue.add(node.left); |
| 131 | + if (node.right != null) queue.add(node.right); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return result; |
| 136 | + |
| 137 | + }} |
| 138 | + ``` |
| 139 | + </TabItem> |
| 140 | + <TabItem value="C++" label="C++"> |
| 141 | + <SolutionAuthor name="@mahek0620"/> |
| 142 | + ```cpp |
| 143 | + |
| 144 | + #include <iostream> |
| 145 | + #include <vector> |
| 146 | + #include <queue> |
| 147 | + |
| 148 | + using namespace std; |
| 149 | + |
| 150 | + class Solution { |
| 151 | + public: |
| 152 | + vector<int> rightSideView(TreeNode* root) { |
| 153 | + vector<int> result; |
| 154 | + if (!root) return result; |
| 155 | + |
| 156 | + queue<TreeNode*> q; |
| 157 | + q.push(root); |
| 158 | + |
| 159 | + while (!q.empty()) { |
| 160 | + int size = q.size(); |
| 161 | + for (int i = 0; i < size; ++i) { |
| 162 | + TreeNode* node = q.front(); |
| 163 | + q.pop(); |
| 164 | + if (i == size - 1) result.push_back(node->val); |
| 165 | + if (node->left) q.push(node->left); |
| 166 | + if (node->right) q.push(node->right); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return result; |
| 171 | + } |
| 172 | + }; |
| 173 | + ``` |
| 174 | + |
| 175 | + </TabItem> |
| 176 | +</Tabs> |
| 177 | + |
| 178 | +### Complexity Analysis |
| 179 | + |
| 180 | +- **Time complexity**: The time complexity of the solution is $O(N)$, where N is the number of nodes in the binary tree. This is because we visit each node exactly once during the level-order traversal. |
| 181 | +- **Space complexity**: The space complexity of the solution is $O(N)$, where N is the number of nodes in the binary tree. This is because the space used by the queue for level-order traversal can be at most N in the worst case. |
| 182 | + |
| 183 | +## References |
| 184 | + |
| 185 | +- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/binary-tree-right-side-view/) |
| 186 | +- **Solution Link:** [Binary Tree Right Side View Solution on LeetCode](https://leetcode.com/problems/binary-tree-right-side-view/solutions/) |
| 187 | +- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
0 commit comments