Skip to content

Commit f9c7cea

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

File tree

1 file changed

+150
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/22 - Binary Tree Right Side View

1 file changed

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