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:

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:

Example 3
// creating a C++ program to print the left view of the binary tree.
#include <bits/stdc++.h>
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->val = record;
temp->Lft = NILL;
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 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 < level)
{
cout << root->val << " ";
*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->Lft, level + 1, max_level);
LftView(root->Rt, level + 1, max_level);
}
// writing the main code.
int main()
{
Nod* root = newNod(1);
root->Lft = newNod(2);
root->Rt = newNod(3);
root->Lft->Lft = newNod(4);
root->Lft->Rt = newNod(5);
root->Rt->Rt = newNod(6);
root->Rt->Rt->Rt = newNod(7);
int max_level = 0;
LftView(root, 1, &max_level);
return 0;
}
Output:

Example 4)
// creating a function to print the left view of the binary tree.
void LftView(Nod* root)
{
if (root==NILL)
return;
queue<Nod*> 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<<q.front()->record<<" ";
// visiting all the nodes from this particular level.
for(int i = 0; i < n; i++)
{
Nod* temp = q.front();
q.pop();
// adding the left node to the list
if (temp->Lft != NILL)
q.push(temp->Lft);
// adding the right node to the list
if (temp->Rt != NILL)
q.push(temp->Rt);
}
}
}
Writing the java code
private static void LftView(Nod root)
{
if (root == NILL)
return;
Queue<Nod> q = new LinkedList<>();
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 < 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:
