Print the outer cone layer
Last Updated :
14 Nov, 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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = nullptr;
return temp;
}
int maxLevel = 0;
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);
}
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);
}
vector< int > coneLevel(Node* root)
{
maxLevel = 0;
vector< int > ans;
leftView(root->left, 1, ans);
reverse(ans.begin(), ans.end());
maxLevel = 0;
rightView(root, 1, ans);
return ans;
}
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);
vector< int > ans = coneLevel(root);
for ( auto it : ans)
cout << it << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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 ;
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);
}
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);
}
public static ArrayList<Integer> coneLevel(Node root)
{
maxLevel = 0 ;
ArrayList<Integer> ans = new ArrayList<>();
leftView(root.left, 1 , ans);
Collections.reverse(ans);
maxLevel = 0 ;
rightView(root, 1 , ans);
return ans;
}
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 );
ArrayList<Integer> ans = coneLevel(root);
for (Integer it : ans)
System.out.print(it + " " );
}
}
|
Python3
class Node:
def __init__( self ,data = 0 ,left = None ,right = None ):
self .data = data
self .left = left
self .right = right
maxLevel = 0
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)
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)
def coneLevel(root):
global maxLevel
maxLevel = 0
ans = []
leftView(root.left, 1 , ans)
ans = ans[:: - 1 ]
maxLevel = 0
rightView(root, 1 , ans)
return ans
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 )
ans = coneLevel(root)
for it in ans:
print (it ,end = " " )
|
C#
using System;
using System.Collections;
public class GFG{
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;
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);
}
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);
}
public static ArrayList coneLevel(Node root)
{
maxLevel = 0;
ArrayList ans = new ArrayList();
leftView(root.left, 1, ans);
ans.Reverse();
maxLevel = 0;
rightView(root, 1, ans);
return ans;
}
static public void Main (){
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);
ArrayList ans = coneLevel(root);
foreach ( int it in ans){
Console.Write(it + " " );
}
}
}
|
Javascript
<script>
class Node{
constructor(data = 0,left = null ,right = null ){
this .data = data
this .left = left
this .right = right
}
}
let maxLevel = 0
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 rightView(root,level,ans){
if (root == null )
return
if (maxLevel < level){
ans.push(root.data)
maxLevel = level
}
rightView(root.right, level + 1, ans)
}
function coneLevel(root){
maxLevel = 0
let ans = []
leftView(root.left, 1, ans)
ans = ans.reverse();
maxLevel = 0
rightView(root, 1, ans)
return ans
}
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)
let ans = coneLevel(root)
for (let it of ans)
document.write(it , " " )
</script>
|
Time Complexity: O(V + E), where V is the vertices and E is the edges
Auxiliary Space: O(V)
Share your thoughts in the comments
Please Login to comment...