|
| 1 | +<h1 align='center'>Left - View - Of - Binary - Tree</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Left View of Binary Tree](https://www.geeksforgeeks.org/problems/left-view-of-binary-tree/1) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Problem Explanation |
| 11 | +The **Left View** of a binary tree is the set of nodes visible when the tree is viewed from the left side. For each level of the tree, the first node encountered from the left side is included in the left view. |
| 12 | + |
| 13 | +For example: |
| 14 | + |
| 15 | +Given the binary tree: |
| 16 | +``` |
| 17 | + 1 |
| 18 | + / \ |
| 19 | + 2 3 |
| 20 | + / \ \ |
| 21 | + 4 5 6 |
| 22 | + / |
| 23 | + 7 |
| 24 | +``` |
| 25 | + |
| 26 | +The **Left View** of this binary tree would be `[1, 2, 4, 7]`. |
| 27 | + |
| 28 | +### Step 2: Approach and Explanation |
| 29 | + |
| 30 | +To solve this problem, we can use a **recursive approach with depth tracking**: |
| 31 | +1. Perform a **preorder traversal** (root -> left -> right) so we visit nodes level by level from left to right. |
| 32 | +2. Track the **current level** of the tree during traversal. |
| 33 | +3. For each level, if this is the first time we reach that level, add the node's data to our answer vector. |
| 34 | +4. By doing this, we ensure that only the leftmost node at each level is added to the left view. |
| 35 | + |
| 36 | +## Problem Solution |
| 37 | +```cpp |
| 38 | +class Solution { |
| 39 | + private: |
| 40 | + void solve(Node* root, vector<int> &ans, int level){ |
| 41 | + if(root == NULL) return; |
| 42 | + |
| 43 | + if(level == ans.size()) ans.push_back(root -> data); |
| 44 | + |
| 45 | + solve(root -> left, ans, level+1); |
| 46 | + solve(root -> right, ans, level+1); |
| 47 | + } |
| 48 | + public: |
| 49 | + vector<int> leftView(Node *root) { |
| 50 | + vector<int> ans; |
| 51 | + |
| 52 | + solve(root, ans, 0); |
| 53 | + |
| 54 | + return ans; |
| 55 | + } |
| 56 | +}; |
| 57 | +``` |
| 58 | +
|
| 59 | +## Problem Solution Explanation |
| 60 | +
|
| 61 | +```cpp |
| 62 | +class Solution { |
| 63 | + private: |
| 64 | + void solve(Node* root, vector<int> &ans, int level){ |
| 65 | + if(root == NULL) return; |
| 66 | +``` |
| 67 | +- 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`). |
| 68 | +- If the `root` is `NULL`, return, as there are no nodes to process. |
| 69 | + |
| 70 | +```cpp |
| 71 | + if(level == ans.size()) ans.push_back(root -> data); |
| 72 | +``` |
| 73 | +- **Condition check**: If `level` is equal to the current size of `ans`, it means we’re at a new level not yet recorded in `ans`. |
| 74 | +- If true, add the `root`'s data to `ans`. This ensures we only add the leftmost node of each level to the result. |
| 75 | + |
| 76 | +```cpp |
| 77 | + solve(root -> left, ans, level+1); |
| 78 | + solve(root -> right, ans, level+1); |
| 79 | + } |
| 80 | +``` |
| 81 | +- Recursively call `solve` on the left child (`root->left`) and right child (`root->right`) with the incremented `level`. |
| 82 | +- This order (left before right) ensures that we always process the leftmost nodes at each level before the right nodes. |
| 83 | +
|
| 84 | +```cpp |
| 85 | + public: |
| 86 | + vector<int> leftView(Node *root) { |
| 87 | + vector<int> ans; |
| 88 | + |
| 89 | + solve(root, ans, 0); |
| 90 | + |
| 91 | + return ans; |
| 92 | + } |
| 93 | +}; |
| 94 | +``` |
| 95 | +- The `leftView` function is the main function that initializes an empty vector `ans` to store the left view nodes. |
| 96 | +- Calls the helper function `solve` with `root`, `ans`, and the initial `level` set to `0`. |
| 97 | +- Finally, it returns `ans`, which now contains the nodes visible from the left view. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +### Step 4: Output Examples with Explanation |
| 102 | + |
| 103 | +Let's consider an example. |
| 104 | + |
| 105 | +#### Example 1 |
| 106 | +Input: |
| 107 | +``` |
| 108 | + 1 |
| 109 | + / \ |
| 110 | + 2 3 |
| 111 | + / \ \ |
| 112 | + 4 5 6 |
| 113 | + / |
| 114 | + 7 |
| 115 | +``` |
| 116 | +- At **level 0**, we add `1` to `ans`. |
| 117 | +- At **level 1**, the leftmost node is `2`, so we add `2` to `ans`. |
| 118 | +- At **level 2**, the leftmost node is `4`, so we add `4` to `ans`. |
| 119 | +- At **level 3**, the leftmost node is `7`, so we add `7` to `ans`. |
| 120 | + |
| 121 | +Output: `[1, 2, 4, 7]` |
| 122 | + |
| 123 | +#### Example 2 |
| 124 | +Input: |
| 125 | +``` |
| 126 | + 10 |
| 127 | + / |
| 128 | + 20 |
| 129 | + \ |
| 130 | + 30 |
| 131 | + \ |
| 132 | + 40 |
| 133 | +``` |
| 134 | +Output: `[10, 20, 30, 40]` |
| 135 | + |
| 136 | +### Step 5: Time and Space Complexity |
| 137 | + |
| 138 | +#### Time Complexity |
| 139 | +- **O(N)**: We visit each node in the tree exactly once, where **N** is the total number of nodes in the tree. |
| 140 | + |
| 141 | +#### Space Complexity |
| 142 | +- **O(H)**: The recursive stack takes up to **H** space, where **H** is the height of the tree. The `ans` vector also takes up **O(H)** space for storing nodes in the left view. |
0 commit comments