Left View of a Binary Tree
Last Updated :
05 Oct, 2024
Given a Binary Tree, the task is to print the left view of the Binary Tree. The left view of a Binary Tree is a set of leftmost nodes for every level.
Examples:
Example 1 : The Green colored nodes represents the left view in the below Binary tree.

Example 2: The Green colored nodes represents the left view in the below Binary tree.

[Expected Approach – 1] Using Recursion – O(n) Time and O(n) Space
The idea is to keep track of the level of a node by passing the level as a parameter to all recursive calls and also keep track of the maximum level. Whenever, we see a node whose level is more than maximum level so far, we print the node because this is the first node in its level .
Note: We traverse the left subtree before right subtree.
Below is the implementation of the above approach:
C++
// C++ program to print left view of Binary Tree
// using resursion
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *left, *right;
Node(int x) {
data = x;
left = right = nullptr;
}
};
// Helper function for the left view using Recursion
void RecLeftView(Node* root, int level,
int& maxLevel, vector<int>& result) {
if (!root) return;
// If current level is more than max level,
// this is the first node of that level
if (level > maxLevel) {
result.push_back(root->data);
maxLevel = level;
}
// Traverse left subtree first, then right subtree
RecLeftView(root->left, level + 1, maxLevel, result);
RecLeftView(root->right, level + 1, maxLevel, result);
}
// Function to return the left view of the binary tree
vector<int> leftView(Node *root) {
vector<int> result;
int maxLevel = -1;
// Start recursion with root at level 0
RecLeftView(root, 0, maxLevel, result);
return result;
}
void printArray(vector<int>& arr) {
for (int val : arr) {
cout << val << " ";
}
cout << endl;
}
int main() {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
vector<int> result = leftView(root);
printArray(result);
return 0;
}
C
// C program to print left view of Binary Tree
// using resursion
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
// Helper function for the left view using Recursion
void RecLeftView(struct Node* root, int level,
int* maxLevel, int* result, int* index) {
if (!root) return;
// If current level is more than max level,
// this is the first node of that level
if (level > *maxLevel) {
result[(*index)++] = root->data;
*maxLevel = level;
}
// Traverse left subtree first, then
// right subtree
RecLeftView(root->left, level + 1, maxLevel, result, index);
RecLeftView(root->right, level + 1, maxLevel, result, index);
}
// Function to return the left view of the binary tree
void leftView(struct Node* root, int* result, int* size) {
int maxLevel = -1;
int index = 0;
// Start recursion with root at level 0
RecLeftView(root, 0, &maxLevel, result, &index);
*size = index;
}
void printArray(int* arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
struct Node* createNode(int x) {
struct Node* newNode
= (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->left = newNode->right = NULL;
return newNode;
}
int main() {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
struct Node *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
int result[100];
int size = 0;
leftView(root, result, &size);
printArray(result, size);
return 0;
}
Java
// Java program to print left view of binary tree
// using Recursion
import java.util.ArrayList;
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = right = null;
}
}
// Helper function for the left view using Recursion
class GfG {
static void RecLeftView(Node root, int level,
int[] maxLevel, ArrayList<Integer> result) {
if (root == null) return;
// If current level is more than max level,
// this is the first node of that level
if (level > maxLevel[0]) {
result.add(root.data);
maxLevel[0] = level;
}
// Traverse left subtree first, then right subtree
RecLeftView(root.left, level + 1, maxLevel, result);
RecLeftView(root.right, level + 1, maxLevel, result);
}
// Function to return the left view of the binary tree
static ArrayList<Integer> leftView(Node root) {
ArrayList<Integer> result = new ArrayList<>();
int[] maxLevel = new int[] {-1};
// Start recursion with root at level 0
RecLeftView(root, 0, maxLevel, result);
return result;
}
static void printArray(ArrayList<Integer> arr) {
for (int val : arr) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
ArrayList<Integer> result = leftView(root);
printArray(result);
}
}
Python
# Python program to print left view of Binary Tree
# using Recursion
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Helper function for the left view using Recursion
def RecLeftView(root, level, maxLevel, result):
if root is None:
return
# If current level is more than max level,
# this is the first node of that level
if level > maxLevel[0]:
result.append(root.data)
maxLevel[0] = level
# Traverse left subtree first, then right subtree
RecLeftView(root.left, level + 1, maxLevel, result)
RecLeftView(root.right, level + 1, maxLevel, result)
# Function to return the left view of the binary tree
def LeftView(root):
result = []
maxLevel = [-1]
# Start recursion with root at level 0
RecLeftView(root, 0, maxLevel, result)
return result
def printArray(arr):
for val in arr:
print(val, end=" ")
print()
if __name__ == "__main__":
# Representation of the input tree:
# 1
# / \
# 2 3
# / \
# 4 5
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
result = LeftView(root)
printArray(result)
C#
// C# program to print left view of binary tree
// using Recursion
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = right = null;
}
}
class GfG{
// Helper function for the left view using Recursion
static void RecLeftView(Node root, int level,
ref int maxLevel, List<int> result) {
if (root == null) return;
// If current level is more than max level,
// this is the first node of that level
if (level > maxLevel) {
result.Add(root.data);
maxLevel = level;
}
// Traverse left subtree first, then right subtree
RecLeftView(root.left, level + 1, ref maxLevel, result);
RecLeftView(root.right, level + 1, ref maxLevel, result);
}
// Function to return the left view of the binary tree
static List<int> LeftView(Node root) {
List<int> result = new List<int>();
int maxLevel = -1;
// Start recursion with root at level 0
RecLeftView(root, 0, ref maxLevel, result);
return result;
}
static void PrintList(List<int> arr) {
foreach (int val in arr) {
Console.Write(val + " ");
}
Console.WriteLine();
}
static void Main(string[] args) {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
List<int> result = LeftView(root);
PrintList(result);
}
}
JavaScript
// Javascript program to print left view
// of binary tree using Recursion
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// Helper function for the left view using Recursion
function RecLeftView(root, level,maxLevel, result) {
if (root === null) return;
// If current level is more than max level,
// this is the first node of that level
if (level > maxLevel[0]) {
result.push(root.data);
maxLevel[0] = level;
}
// Traverse left subtree first, then
// right subtree
RecLeftView(root.left, level + 1, maxLevel, result);
RecLeftView(root.right, level + 1, maxLevel, result);
}
// Function to return the left view of the binary tree
function leftView(root) {
let result = [];
let maxLevel = [-1];
// Start recursion with root at level 0
RecLeftView(root, 0, maxLevel, result);
return result;
}
// Function to print the array
function printArray(arr) {
console.log(arr.join(' '));
}
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
let result = leftView(root);
printArray(result);
Time Complexity: O(n), The function does a simple traversal of the tree, so the complexity is O(n).
Auxiliary Space: O(h), due to the stack space during recursive call. ‘h‘ here is the height of the binary tree.
[Expected Approach – 2] Using Level Order Traversal – O(n) Time and O(n) Space
The left view contains all nodes that are the first nodes in their levels. A simple solution is to do level order traversal and print the first node in every level.
Follow the below step to Implement the idea:
- Do level order traversal of the tree.
- For each level keep a track of the current level and print the first encountered node of this level.
- Move to the next level.
Below is the implementation of the above approach:
C++
// C++ program to print left view of Binary
// tree using Level order Traversal
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int x) {
data = x;
left = right = nullptr;
}
};
// Function to return the left view of the binary tree
vector<int> leftView(Node* root) {
vector<int> result;
if (root == nullptr) return result;
// Queue for level order traversal
queue<Node*> q;
q.push(root);
while (!q.empty()) {
// Number of nodes at current level
int levelSize = q.size();
for (int i = 0; i < levelSize; i++) {
Node* curr = q.front();
q.pop();
// If it's the first node of the current level
if (i == 0) {
result.push_back(curr->data);
}
// Enqueue left child
if (curr->left != nullptr) {
q.push(curr->left);
}
// Enqueue right child
if (curr->right != nullptr) {
q.push(curr->right);
}
}
}
return result;
}
void printArray(vector<int>& arr) {
for (int val : arr) {
cout << val << " ";
}
cout << endl;
}
int main() {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
vector<int> result = leftView(root);
printArray(result);
return 0;
}
Java
// Java program to print left view of Binary
// tree using Level order Traversal
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Function to return the left view of the binary tree
static ArrayList<Integer> leftView(Node root) {
ArrayList<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
// Queue for level order traversal
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
// Number of nodes at the current level
int levelSize = q.size();
for (int i = 0; i < levelSize; i++) {
Node curr = q.poll();
// If it's the first node of the current level
if (i == 0) {
result.add(curr.data);
}
// Enqueue left child
if (curr.left != null) {
q.add(curr.left);
}
// Enqueue right child
if (curr.right != null) {
q.add(curr.right);
}
}
}
return result;
}
static void printArray(ArrayList<Integer> arr) {
for (int val : arr) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
ArrayList<Integer> result = leftView(root);
printArray(result);
}
}
Python
# Python program to print left view of Binary Tree
# using Level Order Traversal
from collections import deque
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# Function to return the left view of the binary tree
def LeftView(root):
result = []
if root is None:
return result
# Queue for level order traversal
q = deque([root])
while q:
# Number of nodes at the current level
level_size = len(q)
for i in range(level_size):
curr = q.popleft()
# If it's the first node of the
# current level
if i == 0:
result.append(curr.data)
# Enqueue left child
if curr.left is not None:
q.append(curr.left)
# Enqueue right child
if curr.right is not None:
q.append(curr.right)
return result
def printArray(arr):
for val in arr:
print(val, end=" ")
print()
if __name__ == "__main__":
# Representation of the input tree:
# 1
# / \
# 2 3
# / \
# 4 5
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
result = LeftView(root)
printArray(result)
C#
// C# program to print left view of Binary
// Tree using Level Order Traversal
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Function to return the left view of
// the binary tree
static List<int> LeftView(Node root) {
List<int> result = new List<int>();
if (root == null) {
return result;
}
// Queue for level order traversal
Queue<Node> queue = new Queue<Node>();
queue.Enqueue(root);
while (queue.Count > 0) {
// Number of nodes at the current level
int levelSize = queue.Count;
for (int i = 0; i < levelSize; i++) {
Node curr = queue.Dequeue();
// If it's the first node of
// the current level
if (i == 0) {
result.Add(curr.data);
}
// Enqueue left child
if (curr.left != null) {
queue.Enqueue(curr.left);
}
// Enqueue right child
if (curr.right != null) {
queue.Enqueue(curr.right);
}
}
}
return result;
}
static void PrintList(List<int> arr) {
foreach (int val in arr) {
Console.Write(val + " ");
}
Console.WriteLine();
}
static void Main(string[] args) {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
List<int> result = LeftView(root);
PrintList(result);
}
}
JavaScript
// Javascript program to print left view of Binary
// tree using Level order Traversal
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// Function to return the left view of the binary tree
function leftView(root) {
let result = [];
if (root === null) {
return result;
}
// Queue for level order traversal
let queue = [root];
while (queue.length > 0) {
// Temporary array for the next level
let currqueue = [];
// Number of nodes at the current level
let levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
let curr = queue[i];
// If it's the first node of the current level
if (i === 0) {
result.push(curr.data);
}
// Enqueue left child
if (curr.left !== null) {
currqueue.push(curr.left);
}
// Enqueue right child
if (curr.right !== null) {
currqueue.push(curr.right);
}
}
// Move to the next level
queue = currqueue;
}
return result;
}
function printArray(arr) {
console.log(arr.join(' '));
}
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
let result = leftView(root);
printArray(result);
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)
[Expected Approach – 3] Using queue and a null pointer – O(n) Time and O(n) Space
The approach involves using a queue along with a null pointer to mark the end of each level in the tree. By inserting a null pointer at the beginning of the queue, we can identify when we reach the start of a new level. When this null pointer is encountered, we set a boolean flag to true and take the next element as the left view element.
Below is the Implementation of the above approach:
C++
// C++ program to print left view of Binary Tree
// using queue and null pointer
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *left, *right;
Node(int x) {
data = x;
left = right = nullptr;
}
};
vector<int> leftView(Node* root) {
vector<int> ans;
// Base Case: Empty Tree
if (!root)
return ans;
// Create an empty queue and enqueue root
// node and null
queue<Node*> q;
q.push(root);
q.push(nullptr);
bool check = true;
// Traverse till queue is not empty
while (!q.empty()) {
// Get the front node and dequeue it from queue
auto curr = q.front();
q.pop();
// If the front node is null, do the
// following steps
if (curr == nullptr) {
if (check == false)
check = true;
if (q.size() == 0)
break;
else
q.push(nullptr);
}
// Else do the following steps
else {
if (check) {
ans.push_back(curr->data);
check = false;
}
if (curr->left)
q.push(curr->left);
if (curr->right)
q.push(curr->right);
}
}
return ans;
}
void printArray(vector<int>& arr) {
for (int val : arr) {
cout << val << " ";
}
cout << endl;
}
int main() {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
vector<int> result = leftView(root);
printArray(result);
return 0;
}
Java
// Java program to print left view of Binary Tree
// using queue and null pointer
import java.util.*;
class Node {
int data;
Node left, right;
Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
// Function to return the left view of
// the binary tree
static ArrayList<Integer> leftView(Node root) {
ArrayList<Integer> ans = new ArrayList<>();
// Base Case: Empty Tree
if (root == null)
return ans;
// Create an empty queue and enqueue
// root node and null
Queue<Node> q = new LinkedList<>();
q.add(root);
q.add(null);
boolean check = true;
// Traverse till queue is not empty
while (!q.isEmpty()) {
// Get the front node and dequeue
// it from queue
Node curr = q.poll();
// If the front node is null, do
// the following steps
if (curr == null) {
if (!check)
check = true;
if (q.isEmpty())
break;
else
q.add(null);
}
// Else do the following steps
else {
if (check) {
ans.add(curr.data);
check = false;
}
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
}
return ans;
}
static void printArray(ArrayList<Integer> arr) {
for (int val : arr) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
ArrayList<Integer> result = leftView(root);
printArray(result);
}
}
Python
# Python program to print left view of Binary Tree
# using queue and null pointer
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def LeftView(root):
# Initialize the list to hold the left view nodes
ans = []
# Base Case: Return an empty list if the tree is empty
if root is None:
return ans
# Create a queue and enqueue the root
# node and a None marker
queue = [root, None]
check = True
# Traverse until the queue is empty
while queue:
# Get the front node from the queue
curr = queue.pop(0)
# If the front node is None, handle the end
# of the current level
if curr is None:
# Break if the queue is empty
if not queue:
break
else:
# Add None for the next level
# and reset check
queue.append(None)
check = True
# If the current node is valid, process it
else:
# If it's the first node of the current
# level, add to the result
if check:
ans.append(curr.data)
check = False
# Enqueue the left child if it exists
if curr.left:
queue.append(curr.left)
# Enqueue the right child if it exists
if curr.right:
queue.append(curr.right)
return ans
def print_array(arr):
print(' '.join(map(str, arr)))
# Representation of the input tree:
# 1
# / \
# 2 3
# / \
# 4 5
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
result = LeftView(root)
print_array(result)
C#
// C# program to print left view of Binary Tree
// using queue and null pointer
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node(int x) {
data = x;
left = right = null;
}
}
class GfG {
static List<int> LeftView(Node root) {
List<int> ans = new List<int>();
// Base Case: Empty Tree
if (root == null)
return ans;
// Create a queue and enqueue the root
// node and a null marker
Queue<Node> queue = new Queue<Node>();
queue.Enqueue(root);
queue.Enqueue(null);
bool check = true;
// Traverse until the queue is empty
while (queue.Count > 0) {
// Get the front node and dequeue it
Node curr = queue.Dequeue();
// If the front node is null, handle the
// end of the current level
if (curr == null) {
if (!check)
check = true;
if (queue.Count == 0)
break;
else
queue.Enqueue(null);
}
else {
// If it's the first node of the current
// level, add to the result
if (check) {
ans.Add(curr.data);
check = false;
}
// Enqueue left and right children
// if they exist
if (curr.left != null)
queue.Enqueue(curr.left);
if (curr.right != null)
queue.Enqueue(curr.right);
}
}
return ans;
}
static void PrintArray(List<int> arr) {
foreach(int val in arr) {
Console.Write(val + " ");
}
Console.WriteLine();
}
static void Main() {
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
List<int> result = LeftView(root);
PrintArray(result);
}
}
JavaScript
// JavaScript program to print left view of Binary Tree
// using queue and null pointer
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
function leftView(root) {
const ans = [];
// Base Case: Empty Tree
if (root === null)
return ans;
// Create a queue and enqueue the root node and a null
// marker
let queue = [ root ];
let check = true;
// Traverse until the queue is empty
while (queue.length > 0) {
const currqueue = [];
const levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
const curr = queue[i];
// If it's the first node of the current level,
// add to the result
if (check) {
ans.push(curr.data);
check = false;
}
// Enqueue left and right children if they exist
if (curr.left !== null)
currqueue.push(curr.left);
if (curr.right !== null)
currqueue.push(curr.right);
}
// Prepare for the next level
queue = currqueue;
check = true;
}
return ans;
}
function printArray(arr) { console.log(arr.join(" ")); }
// Representation of the input tree:
// 1
// / \
// 2 3
// / \
// 4 5
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
const result = leftView(root);
printArray(result);
Time Complexity: O(n), where n is the total number of nodes.
Auxiliary Space: O(n) , due to the space occupied by queue.
Related article:
Similar Reads
Left View of a Binary Tree
Given a Binary Tree, the task is to print the left view of the Binary Tree. The left view of a Binary Tree is a set of leftmost nodes for every level. Examples: Example 1 : The Green colored nodes represents the left view in the below Binary tree. Example 2: The Green colored nodes represents the le
15+ min read
Left View of a Binary Tree Using Stack
Given a Binary Tree, the task is to print the left view of the Binary Tree. The left view of a Binary Tree is a set of leftmost nodes for every level. Examples: Example 1 : The Green colored nodes represents the left view in the below Binary tree. Example 2: The Green colored nodes represents the le
8 min read
Deletion in a Binary Tree
Given a binary tree, the task is to delete a given node from it by making sure that the tree shrinks from the bottom (i.e. the deleted node is replaced by the bottom-most and rightmost node). This is different from BST deletion. Here we do not have any order among elements, so we replace them with t
12 min read
Print Bottom-Right View of a Binary Tree
Given a Binary Tree, print Bottom-Right view of it. The Bottom Right view of a Binary Tree is a set of nodes visible when the tree is visited from Bottom Right side, return the values of the nodes ordered from right to left. In the bottom-right view, on viewing the tree at an angle of 45 degrees fro
11 min read
Left and Right view of a Generic Tree
Given a Generic Tree consisting of N nodes, the task is to find the left and right views of the given generic Tree. Examples: Input: 1 / \ 2 3 / \ / \ \4 5 6 7 8 Output:Left View: 1 2 4 Right View: 1 3 8 Explanation:When the tree is viewed from the left side, then the nodes which are visible is 1 2
9 min read
Types of Binary Tree
We have discussed Introduction to Binary Tree in set 1 and the Properties of Binary Tree in Set 2. In this post, common types of Binary Trees are discussed. Types of Binary Tree based on the number of children:Following are the types of Binary Tree based on the number of children: Full Binary TreeDe
7 min read
Print Nodes in Top View of Binary Tree
Given a binary tree. The task is to find the top view of the binary tree. The top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Note: Return the nodes from the leftmost node to the rightmost node.If two nodes are at the same position (horizontal distance) an
15+ min read
Print Nodes in Top View of Binary Tree
Given a binary tree. The task is to find the top view of the binary tree. The top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Note: Return the nodes from the leftmost node to the rightmost node.If two nodes are at the same position (horizontal distance) an
15+ min read
Sum of nodes in top view of binary tree
Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, the task is to print the sum of nodes in top view.Examples: Input: 1 / \ 2 3 / \ \ 4 5 6 Output: 16 Input: 1 / \ 2 3 \ 4 \ 5 \ 6 Output: 12 Approach: The idea is to put nodes of same hori
13 min read
Perfect Binary Tree
What is a Perfect Binary Tree? A perfect binary tree is a special type of binary tree in which all the leaf nodes are at the same depth, and all non-leaf nodes have two children. In simple terms, this means that all leaf nodes are at the maximum depth of the tree, and the tree is completely filled w
4 min read
Euler tour of Binary Tree
Given a binary tree where each node can have at most two child nodes, the task is to find the Euler tour of the binary tree. Euler tour is represented by a pointer to the topmost node in the tree. If the tree is empty, then value of root is NULL. Examples: Input : Output: 1 5 4 2 4 3 4 5 1 Approach:
7 min read
Sum of nodes in bottom view of Binary Tree
Given a binary tree, the task is to return the sum of nodes in the bottom view of the given Binary Tree. The bottom view of a binary tree is the set of nodes visible when the tree is viewed from the bottom. Note: If there are multiple bottom-most nodes for a horizontal distance from the root, then t
15+ min read
Complete Binary Tree
We know a tree is a non-linear data structure. It has no limitation on the number of children. A binary tree has a limitation as any node of the tree has at most two children: a left and a right child. What is a Complete Binary Tree?A complete binary tree is a special type of binary tree where all t
7 min read
Validate the Binary Tree Nodes
Given N nodes numbered from 0 to n-1, also given the E number of directed edges, determine whether the given nodes all together form a single binary tree or not. Examples: Input: N = 4, edges[][] = [[0 1], [0 2], [2 3]] Output: trueExplanation: Form a single binary tree with a unique root node. Inpu
7 min read
What is Full Binary Tree?
A full binary tree is a type of binary tree in which every node has either zero or two children. Properties of Full Binary Tree: Let i be the number of internal nodes, n be the total number of nodes, l be the number of leaf nodes and h be the total number of levels The number of leaves is (i + 1).Th
2 min read
Find the Level of a Binary Tree with Width K
Given a Binary Tree and an integer K, the task is to find the level of the Binary Tree with width K. If multiple levels exists with width K, print the lowest level. If no such level exists, print -1. The width of a level of a Binary tree is defined as the number of nodes between leftmost and the rig
10 min read
Full Binary Tree
What is a Binary Tree? A binary tree is a tree data structure with a maximum of 2 children per node. We commonly refer to them as the left and right child as each element in a binary tree may only have two children. What is a Full Binary Tree? A full binary tree is a binary tree with either zero or
2 min read
Print nodes in the Top View of Binary Tree | Set 3
Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can be printed in any order. Expected time complexity is O(n) A node x is there in output if x is the topmost node at its horizontal distance. H
9 min read
Vertical width of Binary tree | Set 2
Given a binary tree, find the vertical width of the binary tree. The width of a binary tree is the number of vertical paths. Examples: Input : 7 / \ 6 5 / \ / \ 4 3 2 1 Output : 5 Input : 1 / \ 2 3 / \ / \ 4 5 6 7 \ \ 8 9 Output : 6 Prerequisite: Print Binary Tree in Vertical order In this image, th
5 min read