Check if the Left View of the given tree is sorted or not
Given a tree, our task is to check whether its left view is sorted or not. If it is then return true else false.
Examples:
Input:
Output: true
Explanation:
The left view for the tree would be 10, 20, 50 which is in sorted order.
Approach:
To solve the problem mentioned above we have to perform level order traversal on the tree and look for the very first node of each level. Then initialize a variable to check whether its left view is sorted or not. If it is not sorted, then we can break the loop and print false else loop goes on and at last print true.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int val;
struct node *right, *left;
};
struct node* newnode( int key)
{
struct node* temp = new node;
temp->val = key;
temp->right = NULL;
temp->left = NULL;
return temp;
}
void func(node* root)
{
queue<node*> q;
bool t = true ;
q.push(root);
int i = -1, j = -1, k = -1;
while (!q.empty()) {
int h = q.size();
while (h > 0) {
root = q.front();
if (i == -1) {
j = root->val;
}
if (i == -2) {
if (j <= root->val) {
j = root->val;
i = -3;
}
else {
t = false ;
break ;
}
}
if (root->left != NULL) {
q.push(root->left);
}
if (root->right != NULL) {
q.push(root->right);
}
h = h - 1;
q.pop();
}
i = -2;
if (t == false ) {
break ;
}
}
if (t)
cout << "true" << endl;
else
cout << "false" << endl;
}
int main()
{
struct node* root = newnode(10);
root->right = newnode(50);
root->right->right = newnode(15);
root->left = newnode(20);
root->left->left = newnode(50);
root->left->right = newnode(23);
root->right->left = newnode(10);
func(root);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class node
{
int val;
node right, left;
};
static node newnode( int key)
{
node temp = new node();
temp.val = key;
temp.right = null ;
temp.left = null ;
return temp;
}
static void func(node root)
{
Queue<node> q = new LinkedList<>();
boolean t = true ;
q.add(root);
int i = - 1 , j = - 1 ;
while (!q.isEmpty())
{
int h = q.size();
while (h > 0 )
{
root = q.peek();
if (i == - 1 )
{
j = root.val;
}
if (i == - 2 )
{
if (j <= root.val)
{
j = root.val;
i = - 3 ;
}
else
{
t = false ;
break ;
}
}
if (root.left != null )
{
q.add(root.left);
}
if (root.right != null )
{
q.add(root.right);
}
h = h - 1 ;
q.remove();
}
i = - 2 ;
if (t == false )
{
break ;
}
}
if (t)
System.out.print( "true" + "\n" );
else
System.out.print( "false" + "\n" );
}
public static void main(String[] args)
{
node root = newnode( 10 );
root.right = newnode( 50 );
root.right.right = newnode( 15 );
root.left = newnode( 20 );
root.left.left = newnode( 50 );
root.left.right = newnode( 23 );
root.right.left = newnode( 10 );
func(root);
}
}
|
Python3
class Node:
def __init__( self , key):
self .left = None
self .right = None
self .val = key
def new_node(key):
temp = Node(key)
return temp
def func(root):
q = []
t = True
q.append(root)
i = - 1
j = - 1
while len (q) > 0 :
h = len (q)
while h > 0 :
root = q[ 0 ]
if i = = - 1 :
j = root.val
if i = = - 2 :
if j < = root.val:
j = root.val
i = - 3
else :
t = False
break
if root.left is not None :
q.append(root.left)
if root.right is not None :
q.append(root.right)
h = h - 1
q.pop( 0 )
i = - 2
if t = = False :
break
if t:
print ( "true" )
else :
print ( "false" )
root = new_node( 10 )
root.right = new_node( 50 )
root.right.right = new_node( 15 )
root.left = new_node( 20 )
root.left.left = new_node( 50 )
root.left.right = new_node( 23 )
root.right.left = new_node( 10 )
func(root)
|
C#
using System;
using System.Collections.Generic;
class GFG{
class node
{
public int val;
public node right, left;
};
static node newnode( int key)
{
node temp = new node();
temp.val = key;
temp.right = null ;
temp.left = null ;
return temp;
}
static void func(node root)
{
Queue<node> q = new Queue<node>();
bool t = true ;
q.Enqueue(root);
int i = -1, j = -1;
while (q.Count != 0)
{
int h = q.Count;
while (h > 0)
{
root = q.Peek();
if (i == -1)
{
j = root.val;
}
if (i == -2)
{
if (j <= root.val)
{
j = root.val;
i = -3;
}
else
{
t = false ;
break ;
}
}
if (root.left != null )
{
q.Enqueue(root.left);
}
if (root.right != null )
{
q.Enqueue(root.right);
}
h = h - 1;
q.Dequeue();
}
i = -2;
if (t == false )
{
break ;
}
}
if (t)
Console.Write( "true" + "\n" );
else
Console.Write( "false" + "\n" );
}
public static void Main(String[] args)
{
node root = newnode(10);
root.right = newnode(50);
root.right.right = newnode(15);
root.left = newnode(20);
root.left.left = newnode(50);
root.left.right = newnode(23);
root.right.left = newnode(10);
func(root);
}
}
|
Javascript
<script>
class node
{
constructor(key) {
this .left = null ;
this .right = null ;
this .val = key;
}
}
function newnode(key)
{
let temp = new node(key);
return temp;
}
function func(root)
{
let q = [];
let t = true ;
q.push(root);
let i = -1, j = -1;
while (q.length > 0)
{
let h = q.length;
while (h > 0)
{
root = q[0];
if (i == -1)
{
j = root.val;
}
if (i == -2)
{
if (j <= root.val)
{
j = root.val;
i = -3;
}
else
{
t = false ;
break ;
}
}
if (root.left != null )
{
q.push(root.left);
}
if (root.right != null )
{
q.push(root.right);
}
h = h - 1;
q.shift();
}
i = -2;
if (t == false )
{
break ;
}
}
if (t)
document.write( "true" + "</br>" );
else
document.write( "false" + "</br>" );
}
let root = newnode(10);
root.right = newnode(50);
root.right.right = newnode(15);
root.left = newnode(20);
root.left.left = newnode(50);
root.left.right = newnode(23);
root.right.left = newnode(10);
func(root);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
28 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...