Skip to content

Commit e238f98

Browse files
authored
Create README.md
1 parent 0853122 commit e238f98

File tree

1 file changed

+151
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/21 - Right View of Binary Tree

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<h1 align='center'>Right - View - Of - Binary - Tree</h1>
2+
3+
## Problem Statement
4+
5+
**Problem URL :** [Right View of Binary Tree](https://www.geeksforgeeks.org/problems/right-view-of-binary-tree/1)
6+
7+
![image](https://github.com/user-attachments/assets/2f93fd8c-107d-483f-8eb5-1b34ca78643a)
8+
![image](https://github.com/user-attachments/assets/0363e332-5f00-4931-9eeb-1f6ffeb3e475)
9+
10+
11+
## Problem Explanation
12+
13+
The **Right View** of a binary tree consists of the nodes visible when the tree is viewed from the right side. For each level of the tree, only the rightmost node is included in the right view.
14+
15+
For example:
16+
17+
Given the binary tree:
18+
```
19+
1
20+
/ \
21+
2 3
22+
/ \
23+
4 5
24+
\
25+
6
26+
```
27+
28+
The **Right View** of this binary tree would be `[1, 3, 5, 6]`.
29+
30+
### Step 2: Approach and Explanation
31+
32+
To solve this problem, we use a **recursive approach with depth tracking**:
33+
1. Perform a **modified preorder traversal** where we visit nodes in the order of root -> right -> left. This ensures that we encounter the rightmost nodes first at each level.
34+
2. Track the **current level** as we traverse the tree.
35+
3. For each level, if it is the first time we are encountering that level, we add the node’s data to our result vector (`ans`).
36+
4. This way, only the rightmost node at each level is added to the right view.
37+
38+
## Problem Solution
39+
```cpp
40+
class Solution {
41+
private:
42+
void solve(Node* root, vector<int> &ans, int level){
43+
if(root == NULL) return;
44+
45+
if(level == ans.size()) ans.push_back(root -> data);
46+
47+
solve(root -> right, ans, level+1);
48+
solve(root -> left, ans, level+1);
49+
}
50+
public:
51+
vector<int> rightView(Node *root) {
52+
vector<int> ans;
53+
54+
solve(root, ans, 0);
55+
56+
return ans;
57+
}
58+
};
59+
```
60+
61+
## Problem Solution Explanation
62+
63+
```cpp
64+
class Solution {
65+
private:
66+
void solve(Node* root, vector<int> &ans, int level){
67+
if(root == NULL) return;
68+
```
69+
- Defines a helper function `solve` that takes the current node (`root`), a reference to the answer vector (`ans`), and the current level of the tree (`level`).
70+
- If `root` is `NULL`, return as there are no nodes to process at this point.
71+
72+
```cpp
73+
if(level == ans.size()) ans.push_back(root -> data);
74+
```
75+
- **Condition check**: If `level` equals `ans.size()`, it means we are at a new level that hasn’t been recorded in `ans` yet.
76+
- Adds the `root`'s data to `ans`, ensuring that only the rightmost node of each level is added to the result.
77+
78+
```cpp
79+
solve(root -> right, ans, level+1);
80+
solve(root -> left, ans, level+1);
81+
}
82+
```
83+
- Recursively calls `solve` on the right child (`root->right`) and then on the left child (`root->left`) with an incremented `level`.
84+
- This order (right before left) ensures we always encounter and process the rightmost nodes at each level before the left nodes.
85+
86+
```cpp
87+
public:
88+
vector<int> rightView(Node *root) {
89+
vector<int> ans;
90+
91+
solve(root, ans, 0);
92+
93+
return ans;
94+
}
95+
};
96+
```
97+
- The `rightView` function is the main function that initializes an empty vector `ans` to store the right view nodes.
98+
- Calls the helper function `solve` with `root`, `ans`, and the initial `level` set to `0`.
99+
- Returns `ans`, which now contains the nodes visible from the right view.
100+
101+
---
102+
103+
### Step 4: Output Examples with Explanation
104+
105+
Let's consider some examples to better understand how the function works.
106+
107+
#### Example 1
108+
Input:
109+
```
110+
1
111+
/ \
112+
2 3
113+
/ \
114+
4 5
115+
\
116+
6
117+
```
118+
Explanation:
119+
- **Level 0**: We add `1` to `ans`.
120+
- **Level 1**: The rightmost node is `3`, so we add `3`.
121+
- **Level 2**: The rightmost node is `5`, so we add `5`.
122+
- **Level 3**: The rightmost node is `6`, so we add `6`.
123+
124+
Output: `[1, 3, 5, 6]`
125+
126+
#### Example 2
127+
Input:
128+
```
129+
10
130+
/
131+
20
132+
\
133+
30
134+
\
135+
40
136+
```
137+
Explanation:
138+
- **Level 0**: We add `10`.
139+
- **Level 1**: The rightmost node is `20`.
140+
- **Level 2**: The rightmost node is `30`.
141+
- **Level 3**: The rightmost node is `40`.
142+
143+
Output: `[10, 20, 30, 40]`
144+
145+
### Step 5: Time and Space Complexity
146+
147+
#### Time Complexity
148+
- **O(N)**: We visit each node in the tree exactly once, where **N** is the total number of nodes in the tree.
149+
150+
#### Space Complexity
151+
- **O(H)**: The recursive stack takes up to **H** space, where **H** is the height of the tree. The answer vector (`ans`) will also contain up to **H** nodes, one for each level in the right view.

0 commit comments

Comments
 (0)