|
| 1 | +/// All Views Binary Tree |
| 2 | + |
| 3 | +/// Bottom View (Idea: Consider axis about root, left side -> -ve and right side -> +ve , use map to store the latest occurrence in that level) |
| 4 | + |
| 5 | +class Solution { |
| 6 | + public: |
| 7 | + vector <int> bottomView(Node *root) { |
| 8 | + vector<int> result; |
| 9 | + if(!root) return result; |
| 10 | + map<int,int> mp; /// Col, val |
| 11 | + queue<pair<Node*,int>> q; |
| 12 | + q.push({root,0}); /// Queue {Node and Level (-ve , 0, +ve} |
| 13 | + while(!q.empty()){ |
| 14 | + int sz=q.size(); |
| 15 | + for(int i=0;i<sz;i++){ |
| 16 | + Node *temp=q.front().first; |
| 17 | + int level=q.front().second; |
| 18 | + q.pop(); |
| 19 | + mp[level]=temp->data; |
| 20 | + if(temp->left) q.push({temp->left,level-1}); |
| 21 | + if(temp->right) q.push({temp->right,level+1}); |
| 22 | + } |
| 23 | + } |
| 24 | + for(auto it : mp){ |
| 25 | + result.push_back(it.second); |
| 26 | + } |
| 27 | + return result; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +/// Top View (Idea: Consider axis about root, left side -> -ve and right side -> +ve , use map to store the first occurrence in that level by checking its already presence or not) |
| 32 | + |
| 33 | +class Solution |
| 34 | +{ |
| 35 | + public: |
| 36 | + //Function to return a list of nodes visible from the top view |
| 37 | + //from left to right in Binary Tree. |
| 38 | + vector<int> topView(Node *root) |
| 39 | + { |
| 40 | + vector<int> result; |
| 41 | + if(!root) return result; |
| 42 | + map<int,int> mp; // col,val |
| 43 | + queue<pair<Node*,int>> q; |
| 44 | + q.push({root,0}); |
| 45 | + while(!q.empty()){ |
| 46 | + int sz=q.size(); |
| 47 | + for(int i=0;i<sz;i++){ |
| 48 | + Node *temp=q.front().first; |
| 49 | + int level=q.front().second; |
| 50 | + q.pop(); |
| 51 | + if(mp.find(level)==mp.end()) mp[level]=temp->data; |
| 52 | + if(temp->left) q.push({temp->left,level-1}); |
| 53 | + if(temp->right) q.push({temp->right,level+1}); |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + for(auto it : mp){ |
| 58 | + result.push_back(it.second); |
| 59 | + } |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +/// Vertical Order Traversal (View) // Idea: Maintaining Map (ordered), keep pushing into the level and return into vector afterwards |
| 67 | + |
| 68 | +vector<int> verticalOrder(Node *root) |
| 69 | + { |
| 70 | + vector<int> result; |
| 71 | + if(!root) return result; |
| 72 | + map<int,vector<int>> mp; // col, vector |
| 73 | + queue<pair<Node*,int>> q; // Node, level |
| 74 | + q.push({root,0}); |
| 75 | + while(!q.empty()){ |
| 76 | + int sz=q.size(); |
| 77 | + for(int i=0;i<sz;i++){ |
| 78 | + Node *temp=q.front().first; |
| 79 | + int level=q.front().second; |
| 80 | + q.pop(); |
| 81 | + mp[level].push_back(temp->data); |
| 82 | + if(temp->left) q.push({temp->left,level-1}); |
| 83 | + if(temp->right) q.push({temp->right,level+1}); |
| 84 | + } |
| 85 | + } |
| 86 | + for(auto it : mp){ |
| 87 | + vector<int> temp=it.second; |
| 88 | + for(auto ele : temp){ |
| 89 | + result.push_back(ele); |
| 90 | + } |
| 91 | + } |
| 92 | + return result; |
| 93 | + } |
| 94 | + |
| 95 | + /// Multiset and map based solution (Optimal) |
| 96 | + |
| 97 | + class Solution { |
| 98 | +public: |
| 99 | + vector<vector<int>> verticalTraversal(TreeNode* root) { |
| 100 | + map<int, map<int, multiset<int>>> nodes; |
| 101 | + queue<pair<TreeNode*, pair<int,int>>> q; |
| 102 | + q.push({root, {0,0}}); |
| 103 | + while(!q.empty()){ |
| 104 | + auto p=q.front(); |
| 105 | + q.pop(); |
| 106 | + TreeNode* node=p.first; |
| 107 | + int x=p.second.first, y=p.second.second; |
| 108 | + nodes[x][y].insert(node->val); |
| 109 | + if(node->left){ |
| 110 | + q.push({node->left, {x-1, y+1}}); |
| 111 | + } |
| 112 | + if(node->right){ |
| 113 | + q.push({node->right, {x+1, y+1}}); |
| 114 | + } |
| 115 | + } |
| 116 | + vector<vector<int>> ans; |
| 117 | + for(auto p: nodes){ |
| 118 | + vector<int> col; |
| 119 | + for(auto q: p.second){ |
| 120 | + col.insert(col.end(), q.second.begin(), q.second.end()); |
| 121 | + } |
| 122 | + ans.push_back(col); |
| 123 | + } |
| 124 | + return ans; |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | + |
| 129 | +/// Left Side View (Idea: recursion, start from root, go to left->left , if not then only go left->right, |
| 130 | + |
| 131 | +class Solution{ |
| 132 | +public: |
| 133 | + vector<int> leftSideView(TreeNode* root){ |
| 134 | + vector<int> ans; |
| 135 | + helper(root, 0, ans); |
| 136 | + return ans; |
| 137 | + } |
| 138 | + |
| 139 | + void helper(TreeNode* root,int level,vector<int> &ans){ |
| 140 | + if(root==NULL) |
| 141 | + return ; |
| 142 | + if(ans.size()==level) |
| 143 | + ans.push_back(root->val); |
| 144 | + |
| 145 | + helper(root->left, level+1, ans); |
| 146 | + helper(root->right, level+1, ans); |
| 147 | + } |
| 148 | +}; |
| 149 | + |
| 150 | +/// Left Side View (Queue) |
| 151 | + |
| 152 | +class Solution { |
| 153 | +public: |
| 154 | + vector<int> rightSideView(TreeNode* root) { |
| 155 | + vector<int> res; |
| 156 | + |
| 157 | + if(!root) |
| 158 | + return res; |
| 159 | + |
| 160 | + queue<TreeNode*> q; |
| 161 | + q.push(root); |
| 162 | + while(!q.empty()) |
| 163 | + { |
| 164 | + int n=q.size(); |
| 165 | + for(int i=0;i<n;i++) |
| 166 | + { |
| 167 | + TreeNode * node = q.front(); |
| 168 | + q.pop(); |
| 169 | + if(node->right) |
| 170 | + q.push(node->right); |
| 171 | + if(node->left) |
| 172 | + q.push(node->left); |
| 173 | + if(i==n-1) |
| 174 | + res.push_back(node->val); |
| 175 | + } |
| 176 | + } |
| 177 | + return res; |
| 178 | + } |
| 179 | +}; |
| 180 | + |
| 181 | + |
| 182 | +/// Right Side View (Idea: recursion, start from root, go to right->right , if not then only go right->left, |
| 183 | + |
| 184 | +class Solution { |
| 185 | +public: |
| 186 | + void helper(TreeNode *root, int level, vector<int> &result){ |
| 187 | + if(!root) return; |
| 188 | + if(result.size()==level){ |
| 189 | + result.push_back(root->val); |
| 190 | + } |
| 191 | + helper(root->right,level+1,result); |
| 192 | + helper(root->left,level+1,result); |
| 193 | + } |
| 194 | + vector<int> rightSideView(TreeNode* root) { |
| 195 | + vector<int> result; |
| 196 | + if(!root) return result; |
| 197 | + helper(root,0,result); |
| 198 | + return result; |
| 199 | + } |
| 200 | +}; |
| 201 | + |
| 202 | +/// Right Side View (Queue) |
| 203 | + |
| 204 | +class Solution { |
| 205 | +public: |
| 206 | + vector<int> rightSideView(TreeNode* root) { |
| 207 | + vector<int> res; |
| 208 | + |
| 209 | + if(!root) |
| 210 | + return res; |
| 211 | + |
| 212 | + queue<TreeNode*> q; |
| 213 | + q.push(root); |
| 214 | + while(!q.empty()) |
| 215 | + { |
| 216 | + int n=q.size(); |
| 217 | + for(int i=0;i<n;i++) |
| 218 | + { |
| 219 | + TreeNode * node = q.front(); |
| 220 | + q.pop(); |
| 221 | + if(node->left) |
| 222 | + q.push(node->left); |
| 223 | + if(node->right) |
| 224 | + q.push(node->right); |
| 225 | + if(i==n-1) |
| 226 | + res.push_back(node->val); |
| 227 | + } |
| 228 | + } |
| 229 | + return res; |
| 230 | + } |
| 231 | +}; |
| 232 | + |
| 233 | +/// Boundary View : (Idea : Combination of left side + leaf nodes + right side view) |
| 234 | + |
| 235 | +bool isLeaf(Node *root){ |
| 236 | + if(!root->left and !root->right) return true; |
| 237 | + |
| 238 | +return false; |
| 239 | +} |
| 240 | + |
| 241 | +void addLeftBoundary(Node *root, vector<int> &result){ |
| 242 | + Node *curr=root->left; |
| 243 | + while(curr){ |
| 244 | + if(!isLeaf(curr)) result.push_back(curr->data); |
| 245 | + if(curr->left) curr=curr->left; |
| 246 | + else curr=curr->right; |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +void addLeaves(Node *root, vector<int> &result){ |
| 251 | + if(isLeaf(root)){ |
| 252 | + result.push_back(root->data); |
| 253 | + return; |
| 254 | + } |
| 255 | + if(root->left) addLeaves(root->left,result); |
| 256 | + if(root->right) addLeaves(root->right,result); |
| 257 | +} |
| 258 | + |
| 259 | +void addRightBoundary(Node *root, vector<int> &result){ |
| 260 | + Node *curr=root->right; |
| 261 | + vector<int> temp; |
| 262 | + while(curr){ |
| 263 | + if(!isLeaf(curr)) temp.push_back(curr->data); |
| 264 | + if(curr->right) curr=curr->right; |
| 265 | + else curr=curr->left; |
| 266 | + } |
| 267 | + for(int i=temp.size()-1;i>=0;i--){ |
| 268 | + result.push_back(temp[i]); |
| 269 | + } |
| 270 | +} |
| 271 | + |
| 272 | +class Solution { |
| 273 | +public: |
| 274 | + vector <int> boundary(Node *root) |
| 275 | + { |
| 276 | + vector<int> result; |
| 277 | + if(!root) return result; |
| 278 | + if(!isLeaf(root)) result.push_back(root->data); |
| 279 | + |
| 280 | + addLeftBoundary(root,result); |
| 281 | + |
| 282 | + addLeaves(root,result); |
| 283 | + |
| 284 | + addRightBoundary(root,result); |
| 285 | + |
| 286 | + return result; |
| 287 | + } |
| 288 | +}; |
| 289 | + |
0 commit comments