Open In App

Print the outer cone layer

Last Updated : 14 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 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;
}


Java




// Java code to implement the approach
import java.io.*;
import java.util.*;
 
class GFG {
    // Structure of a binary tree node
    public static class Node {
        int data;
        Node left;
        Node right;
        Node() {}
        Node(int data) { this.data = data; }
        Node(int data, Node left, Node right)
        {
            this.data = data;
            this.left = left;
            this.right = right;
        }
    }
    static int maxLevel = 0;
 
    // Function to find the leftview
    public static void leftView(Node root, int level,
                                ArrayList<Integer> ans)
    {
        if (root == null)
            return;
        if (maxLevel < level) {
            ans.add(root.data);
            maxLevel = level;
        }
        leftView(root.left, level + 1, ans);
    }
 
    // Function to find the rightview
    public static void rightView(Node root, int level,
                                 ArrayList<Integer> ans)
    {
        if (root == null)
            return;
        if (maxLevel < level) {
            ans.add(root.data);
            maxLevel = level;
        }
        rightView(root.right, level + 1, ans);
    }
 
    // Function to print the traversal
    public static ArrayList<Integer> coneLevel(Node root)
    {
        maxLevel = 0;
 
        // ArrayList in which we will store
        // the boundary of the binary tree
        ArrayList<Integer> ans = new ArrayList<>();
 
        // Calling the leftView If you have noticed
        // 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 solution because
        // of the reason stated in step 2
        Collections.reverse(ans);
 
        // 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
    public static void main(String[] args)
    {
        Node root = new Node(12);
        root.left = new Node(15);
        root.right = new Node(65);
        root.left.left = new Node(9);
        root.left.right = new Node(10);
        root.right.left = new Node(46);
        root.right.right = new Node(57);
 
        // Function call
        ArrayList<Integer> ans = coneLevel(root);
 
        // Printing the solution
        for (Integer it : ans)
            System.out.print(it + " ");
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python3 code to implement the approach
 
# Structure of a binary tree node
class Node:
    def __init__(self,data = 0,left = None,right = None):
     
        self.data = data
        self.left = left
        self.right = right
 
maxLevel = 0
 
# Function to find the leftview
def leftView(root,level,ans):
 
    global maxLevel
 
    if (root == None):
        return
    if (maxLevel < level):
        ans.append(root.data)
        maxLevel = level
     
    leftView(root.left, level + 1, ans)
 
# Function to find the rightview
def rightView(root,level,ans):
 
    global maxLevel
 
    if (root == None):
        return
    if (maxLevel < level):
        ans.append(root.data)
        maxLevel = level
    rightView(root.right, level + 1, ans)
 
    # Function to print the traversal
def coneLevel(root):
 
    global maxLevel
     
    maxLevel = 0
 
    # ArrayList in which we will store
    # the boundary of the binary tree
    ans = []
 
    # Calling the leftView If you have noticed
    # 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 solution because
    # of the reason stated in step 2
    ans = ans[::-1]
 
    # 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
root = Node(12)
root.left = Node(15)
root.right = Node(65)
root.left.left = Node(9)
root.left.right = Node(10)
root.right.left = Node(46)
root.right.right = Node(57)
 
# Function call
ans = coneLevel(root)
 
# Printing the solution
for it in ans:
    print(it ,end = " ")
 
# This code is contributed by shinjanpatra


C#




// C# code to implement the approach
using System;
using System.Collections;
 
public class GFG{
 
  // Structure of a binary tree node
  public class Node {
    public int data;
    public Node left;
    public Node right;
    public Node() {}
    public Node(int data) { this.data = data; }
    public Node(int data, Node left, Node right)
    {
      this.data = data;
      this.left = left;
      this.right = right;
    }
  }
  static int maxLevel = 0;
 
  // Function to find the leftview
  public static void leftView(Node root, int level, ArrayList ans)
  {
    if (root == null)
      return;
    if (maxLevel < level) {
      ans.Add(root.data);
      maxLevel = level;
    }
    leftView(root.left, level + 1, ans);
  }
 
  // Function to find the rightview
  public static void rightView(Node root, int level, ArrayList ans)
  {
    if (root == null)
      return;
    if (maxLevel < level) {
      ans.Add(root.data);
      maxLevel = level;
    }
    rightView(root.right, level + 1, ans);
  }
 
  // Function to print the traversal
  public static ArrayList coneLevel(Node root)
  {
    maxLevel = 0;
 
    // ArrayList in which we will store
    // the boundary of the binary tree
    ArrayList ans = new ArrayList();
 
    // Calling the leftView If you have noticed
    // 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 solution because
    // of the reason stated in step 2
    ans.Reverse();
 
    // 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;
  }
 
  static public void Main (){
 
    // Code
    Node root = new Node(12);
    root.left = new Node(15);
    root.right = new Node(65);
    root.left.left = new Node(9);
    root.left.right = new Node(10);
    root.right.left = new Node(46);
    root.right.right = new Node(57);
 
    // Function call
    ArrayList ans = coneLevel(root);
 
    // Printing the solution
    foreach(int it in ans){
      Console.Write(it + " ");
    }
  }
}
 
// This code is contributed by lokeshmvs21.


Javascript




<script>
 
// JavaScript code to implement the approach
 
// Structure of a binary tree node
class Node{
    constructor(data = 0,left = null,right = null){
        this.data = data
        this.left = left
        this.right = right
    }
}
 
let maxLevel = 0
 
// Function to find the leftview
function leftView(root,level,ans){
 
    if (root == null)
        return
    if (maxLevel < level){
        ans.push(root.data)
        maxLevel = level
    }
     
    leftView(root.left, level + 1, ans)
}
 
// Function to find the rightview
function rightView(root,level,ans){
 
    if (root == null)
        return
    if (maxLevel < level){
        ans.push(root.data)
        maxLevel = level
    }
    rightView(root.right, level + 1, ans)
}
 
  // Function to print the traversal
function coneLevel(root){
     
    maxLevel = 0
 
    // ArrayList in which we will store
    // the boundary of the binary tree
    let ans = []
 
    // Calling the leftView If you have noticed
    // 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 solution because
    // of the reason stated in step 2
    ans = ans.reverse();
 
    // 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
let root = new Node(12)
root.left = new Node(15)
root.right = new Node(65)
root.left.left = new Node(9)
root.left.right = new Node(10)
root.right.left = new Node(46)
root.right.right = new Node(57)
 
// Function call
let ans = coneLevel(root)
 
// Printing the solution
for(let it of ans)
    document.write(it ," ")
 
// This code is contributed by shinjanpatra
 
</script>


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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads