The Wayback Machine - https://web.archive.org/web/20220505162541/https://www.geeksforgeeks.org/print-the-outer-cone-layer/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Print the outer cone layer

  • Last Updated : 05 May, 2022

Given a Binary tree, the task is to print the outer cone layer i.e. combination of layers formed by moving only through the left child from the root and the layer moving only through the right child from the root. Print the left layer in bottom-up manner and the right layer in top-down manner.

Examples:

Input:        12
               /     \
           15       65
        /     \      /   \
     9       10 45    57
Output: 9 15 12 65 57
Explanation: The left layer is 12, 15, 9.
In bottom up manner it is 9 15 12.
The right layer is 65, 57.
So the overall layer is 9, 15, 12, 65, 57.

Input:          12
                   /
                4
              /
            5
             \
               3
             /
         1
Output: 5 4 12. 
Explanation: The left layer moving only through the left child is 12, 4, 5.
So the cone layer is 5 4 12

 

Approach: The problem can be solved based on the following idea:

Find the left view of the tree till you can move only through the left child. Similarly, find the right view of the tree till you can move only through the right child. Then print them as mentioned in the problem.

Follow the steps mentioned below to implement the idea:

Step 1: First try to get the leftmost view of the tree such that at any level if there is a node that is the right child then we should not take that.

Reason: Consider the below Tree:

                    12
                   /
                 4
                /
              5
               \
                3
               /
             1

If you try to find the left view of the tree shown above then you will get 12, 4, 5, 3, 1 as the answer. But according to our problem statement, we don’t need 3 and 1.

Step 2: Once you get the left view from step 1, then reverse the values of the left view to get it in a bottom-up manner.

Step 3:  Try to get the rightmost view of the tree such that at any level if there is a node that is a left child then do not take that node in the right view. (The reason is similar to step 1 because it may incorporate nodes that are not needed).  

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Structure of a binary tree node
struct Node {
    int data;
    struct Node *left, *right;
};
  
// Function to create a new tree node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = nullptr;
    return temp;
}
  
int maxLevel = 0;
  
// Function to find the leftview
void leftView(Node* root, int level,
              vector<int>& ans)
{
    if (!root)
        return;
    if (maxLevel < level) {
        ans.push_back(root->data);
        maxLevel = level;
    }
    leftView(root->left, level + 1, ans);
}
  
// Function to find the rightview
void rightView(Node* root, int level,
               vector<int>& ans)
{
    if (!root)
        return;
    if (maxLevel < level) {
        ans.push_back(root->data);
        maxLevel = level;
    }
    rightView(root->right, level + 1, ans);
}
  
// Function to print the traversal
vector<int> coneLevel(Node* root)
{
    maxLevel = 0;
  
    // Vector in which we will store
    // the boundary of the binary tree
    vector<int> ans;
  
    // Calling the leftView If you have notices
    // that we are passing the root->left because
    // we are considering the root value
    // in the rightView call
    leftView(root->left, 1, ans);
  
    // We need to reverse the the solution because
    // of the reason stated in step 2
    reverse(ans.begin(), ans.end());
  
    // Again setting the value of maxLevel as zero
    maxLevel = 0;
  
    // Calling the rightView in this call we are
    // considering the root value
    rightView(root, 1, ans);
    return ans;
}
  
// Driver code
int main()
{
    Node* root = newNode(12);
    root->left = newNode(15);
    root->right = newNode(65);
    root->left->left = newNode(9);
    root->left->right = newNode(10);
    root->right->left = newNode(46);
    root->right->right = newNode(57);
  
    // Function call
    vector<int> ans = coneLevel(root);
  
    // Printing the solution
    for (auto it : ans)
        cout << it << " ";
    return 0;
}
Output
9 15 12 65 57 

Time Complexity: O(V + E), where V is the vertices and E is the edges
Auxiliary Space: O(V)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!