Left and Right view of a Generic Tree
Given a Generic Tree consisting of N nodes, the task is to find the left and right views of the given generic Tree.
Examples:
Input:
1
/ \
2 3
/ \ / \ \
4 5 6 7 8Output:
Left View: 1 2 4
Right View: 1 3 8
Explanation:
When the tree is viewed from the left side, then the nodes which are visible is 1 2 4.
When the tree is viewed from the right side, then the nodes which are visible is 1 3 8.Input:
1
/ \
2 3
\ / \
4 5 6
\
7
Output:
Left View: 1 2 4 7
Right View: 1 3 6 7
Approach: The given problem can be solved by using the concept of Level order traversal of the tree. In this, traversal of the tree is done in level ordered fashion. After storing all the nodes in the level order traversal, it is clear that in the left view of the tree is the first node of all the levels and similarly right view of the tree is the last node of all the levels starting from the root.
Therefore, after storing the level order traversal first print all the first nodes stored in each level for the Left View and print all the last nodes stored in each level for the Right View.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Structure of Nodestruct Node { int val; vector<Node*> child;}; // Utility function to create a// new tree nodeNode* newNode(int key){ Node* temp = new Node; temp->val = key; return temp;} // Function to get the left view and// right view of the given treevoid getLeftRightview(Node* root){ // Stores the nodes of each level queue<Node*> curNodes; // Push the root to initiate the // level ordered traversal curNodes.push(root); // Stores the left and right views vector<int> leftView, rightView; // Iterate until queue is non-empty while (!curNodes.empty()) { // Find the number of ndoes in // current level int n = curNodes.size(); for (int i = 0; i < n; i++) { Node* cur = curNodes.front(); curNodes.pop(); // If the node is first node // in the level if (i == 0) leftView.push_back( cur->val); // If the node is last node // in the level if (i == n - 1) rightView.push_back( cur->val); // Push all the childs of the // current node into the queue for (int it = 0; it < cur->child.size(); it++) { curNodes.push( cur->child[it]); } } } // Print the left view cout << "Left View: "; for (int i = 0; i < leftView.size(); i++) cout << leftView[i] << ' '; cout << endl; // Print the right view cout << "RIght View: "; for (int i = 0; i < rightView.size(); i++) cout << rightView[i] << ' ';} // Driver Codeint main(){ Node* root = newNode(1); (root->child).push_back(newNode(2)); (root->child).push_back(newNode(3)); (root->child[0]->child).push_back(newNode(4)); (root->child[1]->child).push_back(newNode(6)); (root->child[0]->child).push_back(newNode(5)); (root->child[1])->child.push_back(newNode(7)); (root->child[1]->child).push_back(newNode(8)); getLeftRightview(root); return 0;} |
Left View: 1 2 4 RIght View: 1 3 8
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


