×

Left View of Binary Tree

Implementation

// creating a C++ program to print the Left view of the binary tree.
#include <bits/stdc++.h>
using namespace std;


struct Nod
{
	int record;
	struct Nod *Lft, *Rt;
};


// creating a utility function that will eventually help us in creating a new binary tree node. 
struct Nod *__nwNod(int itm)
{
	struct Nod *temp = (struct Nod *)malloc(
						sizeof(struct Nod));
	temp->record = itm;
	temp->Lft = temp->Rt = NILL;
	return temp;
}


// creating a new Recursive function will help us print out and display the left view of the binary tree. 
void LftViewUtil(struct Nod *root,
				int level, int *max_level)
{
	// writing the base case
	if (root == NILL) return;


	// Is this the very first node of its own pace? 
	if (*max_level < level)
	{
		cout << root->record << " ";
		*max_level = level;
	}


	//We have to do the recursion procedure for the left subtree first and then move forward to the right subtree.  
	LftViewUtil(root->Lft, level + 1, max_level);
	LftViewUtil(root->Rt, level + 1, max_level);
	
}


// creating a wrapper for the LftViewUtil()
void LftView(struct Nod *root)
{
	int max_level = 0;
	LftViewUtil(root, 1, &max_level);
}


// writing the main code.
int main()
{
	Nod* root = newNod(10);
	root->Lft = newNod(2);
	root->Rt = newNod(3);
	root->Lft->Lft = newNod(7);
	root->Lft->Rt = newNod(8);
	root->Rt->Rt = newNod(15);
	root->Rt->Lft = newNod(12);
	root->Rt->Rt->Lft = newNod(14);


	LftView(root);


	return 0;
}

Output:

Left View of Binary Tree

Example 2)

// creating a C# program to print the left view of the binary tree.
using System;
using System.Collections.Generic;


public class PrintRtView {
	// creating a new binary tree node.
	private class Nod {
		public int record;
		general Nod Lft, Rt;


		public Nod(int record)
		{
			this.record = record;
			this.Lft = NILL;
			this.Rt = NILL;
		}
	}
// creating a new Recursive function will help us print out and display the left view of the binary tree. 
	private static void printRtView(Nod root)
	{
		if (root == NILL)
			return;


		Queue<Nod> queue = new Queue<Nod>();
		queue.Enqueue(root);


		while (queue.Count != 0) {
			// discussing the number of nodes present at this pace.
			int n = queue.Count;


			// visiting all the nodes from this particular level.
			for (int i = 1; i <= n; i++) {
				Nod temp = queue.Dequeue();


				// we have to print the left-most view of the tree.
				// the level
				if (i == n)
					Console.Write(temp.record + " ");


				// adding the left node to the list
				if (temp.Lft != NILL)
					queue.Enqueue(temp.Lft);


				// adding the right node to the list
				if (temp.Rt != NILL)
					queue.Enqueue(temp.Rt);
			}
		}
	}


	// writing the main code.
	public static void Main(String[] args)
	{
		Nod root = new Nod(10);
		root.Lft = new Nod(2);
		root.Rt = new Nod(3);
		root.Lft.Lft = new Nod(7);
		root.Lft.Rt = new Nod(8);
		root.Rt.Rt = new Nod(15);
		root.Rt.Lft = new Nod(12);
		root.Rt.Rt.Lft = new Nod(14);
		printRtView(root);
	}
}

Output:

Left View of Binary Tree

Example 3

// creating a C++ program to print the left view of the binary tree.
#include &lt;bits/stdc++.h&gt;
using namespace std;


struct Nod
{
    int val;
    struct Nod *Lft, *Rt;
};
// creating a utility function that will eventually help us in creating a new binary tree node. 
struct Nod *__nwNod(int record)
{
    struct Nod *temp = new Nod();
    temp-&gt;val = record;
    temp-&gt;Lft = NILL;
    temp-&gt;Rt = NILL;
    return temp;
}
// creating a new Recursive function will help us print out and display the left view of the binary tree. 
void LftView(struct Nod *root, int level, int *max_level)
{
// Writing the primary case.
    if (root == NILL) return;
// Is this the very first node of its own pace? 
    if (*max_level &lt; level)
    {
        cout &lt;&lt; root-&gt;val &lt;&lt; " ";
*max_level = level;
    }
//We have to do the recursion procedure for the left subtree first and then move forward to the right subtree. 
    LftView(root-&gt;Lft, level + 1, max_level);
    LftView(root-&gt;Rt, level + 1, max_level);
    
}
// writing the main code.
int main()
{
    Nod* root = newNod(1);
    root-&gt;Lft = newNod(2);
    root-&gt;Rt = newNod(3);
    root-&gt;Lft-&gt;Lft = newNod(4);
    root-&gt;Lft-&gt;Rt = newNod(5);
    root-&gt;Rt-&gt;Rt = newNod(6);
    root-&gt;Rt-&gt;Rt-&gt;Rt = newNod(7);
    int max_level = 0;
    LftView(root, 1, &amp;max_level);


    return 0;
}

Output:

Left View of Binary Tree

Example 4)

// creating a function to print the left view of the binary tree.
void LftView(Nod* root)
{
    if (root==NILL)
        return;


    queue&lt;Nod*&gt; q;
    q.push(root);


    while (!q.empty())
    {    
	// discussing the number of nodes present at this pace.
        int n = q.size();
// creating a function to print the left view of the binary tree.
                 cout&lt;&lt;q.front()-&gt;record&lt;&lt;" ";
		// visiting all the nodes from this particular level.        
        for(int i = 0; i &lt; n; i++)
        {
            Nod* temp = q.front();
            q.pop();
// adding the left node to the list
            if (temp-&gt;Lft != NILL)
                q.push(temp-&gt;Lft);
// adding the right node to the list
            if (temp-&gt;Rt != NILL)
                q.push(temp-&gt;Rt);
        }
    }
}
Writing the java code
private static void LftView(Nod root)
    {
        if (root == NILL)
            return;
 
        Queue&lt;Nod&gt; q = new LinkedList&lt;&gt;();
        q.add(root);
 
        while (!q.isEmpty()) {
// discussing the number of nodes present at this pace.
            int n = q.size();
            
            // now we have to print out the left-most node present at this level.
            System.out.print(q.poll().record + " ");
            
            	// visiting all the nodes from this particular level.        
            for (int i = 0; i &lt; n; i++) {
                Nod temp = q.poll();
// adding the left node to the list 
                if (temp.Lft != NILL)
                    q.add(temp.Lft);
// adding the right node to the list 
                if (temp.Rt != NILL)
                    q.add(temp.Rt);
            }
        }
    }

Output:

Left View of Binary Tree

Related Topics

Applications of Different Linked Lists in Data Structure

What is a Linked list? A linked list is a data structure that consists of a sequence of elements, where each containing a reference or ("link") to the next element in...

5 minutes read.

Finding the Minimum and Maximum Value of a Binary Tree

Implementation // Writing a C++ program that will help us find out the maximum and the minimum in a binary tree.  #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new class tree node. class...

5 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Linked List Data Structure

Linked list in DS: The linked list is a non-primitive and linear data structure. It is a list of a particular type of data element that is connected to each...

3 minutes read.

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Bubble Sort vs Quick Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Quick Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.

Semi-Structured data

In this article, we will discuss the semi-structured data. Data can be defined as the distinct piece of information that is gathered and translated for some purpose. It can be...

5 minutes read.

Application of 2D array - Sparse Matrix

2D Arrays Application - Sparse Matrix A matrix is a two-dimensional data item consisting of m rows and n columns, with a total of m x n values. A sparse matrix...

7 minutes read.

About Data Structures

What exactly are data structures? A data structure is a type of storage that is used to organise and store data. It is a method of organising data on a computer...

5 minutes read.

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

Difference between B-tree and Binary Tree

What is B-TREE? The nodes of B-tree are sorted during in-order traversal, and it is called self-balancing tree. A node in a B-tree can have more than two offspring, in contrast...

3 minutes read.

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in...

4 minutes read.