给定一棵树,我们的任务是检查其左视图是否已排序。如果为true,则返回true,否则返回false。
例子:
Input:
Output: true
Explanation:
The left view for the tree would be 10, 20, 50 which is in sorted order.
方法:
为了解决上述问题,我们必须在树上执行级别顺序遍历,并寻找每个级别的第一个节点。然后初始化变量以检查其左视图是否已排序。如果未排序,则可以中断循环并输出false,否则循环将继续,最后打印为true。
下面是上述方法的实现:
C++
// C++ implementation to Check if the Left
// View of the given tree is Sorted or not
#include
using namespace std;
// Binary Tree Node
struct node {
int val;
struct node *right, *left;
};
// Utility function to create a new node
struct node* newnode(int key)
{
struct node* temp = new node;
temp->val = key;
temp->right = NULL;
temp->left = NULL;
return temp;
}
// Fucntion to find left view
// and check if it is sorted
void func(node* root)
{
// queue to hold values
queue q;
// variable to check whether
// level order is sorted or not
bool t = true;
q.push(root);
int i = -1, j = -1, k = -1;
// Iterate until the queue is empty
while (!q.empty()) {
int h = q.size();
// Traverse every level in tree
while (h > 0) {
root = q.front();
// variable for initial level
if (i == -1) {
j = root->val;
}
// checking values are sorted or not
if (i == -2) {
if (j <= root->val) {
j = root->val;
i = -3;
}
else {
t = false;
break;
}
}
// Push left value if it is not null
if (root->left != NULL) {
q.push(root->left);
}
// Push right value if it is not null
if (root->right != NULL) {
q.push(root->right);
}
h = h - 1;
// Pop out the values from queue
q.pop();
}
i = -2;
// Check if the value are not
// sorted then break the loop
if (t == false) {
break;
}
}
if (t)
cout << "true" << endl;
else
cout << "false" << endl;
}
// Driver code
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
// Java implementation to check if the left
// view of the given tree is sorted or not
import java.util.*;
class GFG{
// Binary Tree Node
static class node
{
int val;
node right, left;
};
// Utility function to create a new node
static node newnode(int key)
{
node temp = new node();
temp.val = key;
temp.right = null;
temp.left = null;
return temp;
}
// Function to find left view
// and check if it is sorted
static void func(node root)
{
// Queue to hold values
Queue q = new LinkedList<>();
// Variable to check whether
// level order is sorted or not
boolean t = true;
q.add(root);
int i = -1, j = -1;
// Iterate until the queue is empty
while (!q.isEmpty())
{
int h = q.size();
// Traverse every level in tree
while (h > 0)
{
root = q.peek();
// Variable for initial level
if (i == -1)
{
j = root.val;
}
// Checking values are sorted or not
if (i == -2)
{
if (j <= root.val)
{
j = root.val;
i = -3;
}
else
{
t = false;
break;
}
}
// Push left value if it is not null
if (root.left != null)
{
q.add(root.left);
}
// Push right value if it is not null
if (root.right != null)
{
q.add(root.right);
}
h = h - 1;
// Pop out the values from queue
q.remove();
}
i = -2;
// Check if the value are not
// sorted then break the loop
if (t == false)
{
break;
}
}
if (t)
System.out.print("true" + "\n");
else
System.out.print("false" + "\n");
}
// Driver code
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);
}
}
// This code is contributed by gauravrajput1
C#
// C# implementation to check if the left
// view of the given tree is sorted or not
using System;
using System.Collections.Generic;
class GFG{
// Binary Tree Node
class node
{
public int val;
public node right, left;
};
// Utility function to create a new node
static node newnode(int key)
{
node temp = new node();
temp.val = key;
temp.right = null;
temp.left = null;
return temp;
}
// Function to find left view
// and check if it is sorted
static void func(node root)
{
// Queue to hold values
Queue q = new Queue();
// Variable to check whether
// level order is sorted or not
bool t = true;
q.Enqueue(root);
int i = -1, j = -1;
// Iterate until the queue is empty
while (q.Count != 0)
{
int h = q.Count;
// Traverse every level in tree
while (h > 0)
{
root = q.Peek();
// Variable for initial level
if (i == -1)
{
j = root.val;
}
// Checking values are sorted or not
if (i == -2)
{
if (j <= root.val)
{
j = root.val;
i = -3;
}
else
{
t = false;
break;
}
}
// Push left value if it is not null
if (root.left != null)
{
q.Enqueue(root.left);
}
// Push right value if it is not null
if (root.right != null)
{
q.Enqueue(root.right);
}
h = h - 1;
// Pop out the values from queue
q.Dequeue();
}
i = -2;
// Check if the value are not
// sorted then break the loop
if (t == false)
{
break;
}
}
if (t)
Console.Write("true" + "\n");
else
Console.Write("false" + "\n");
}
// Driver code
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);
}
}
// This code is contributed by gauravrajput1
输出:
true
时间复杂度: O(N)