Middle To Up-Down Order traversal of a Binary Tree
Last Updated :
05 Jan, 2023
Given a binary tree, the task is to traverse this binary tree from the middle to the up-down order.
In Middle to up-down order traversal, the following steps are performed:
- First, print the middle level of the tree.
- Then, print the elements at one level above the middle level of the tree.
- Then, print the elements at one level below the middle level of the tree.
- Then, print the elements at two levels above the middle level of the tree.
- Then, print the elements at two levels below the middle level of the tree and so on.
Note: In this problem, the middle level is considered at ((H / 2) + 1)th level where H is the height of the ree and the level of the root is considered as 1.
Examples:
Input:
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
Output:
14, 15,
12, 13,
21, 22, 23, 24,
10,
Explanation:
There are 4 levels in the tree.
Therefore, Middle level = ((4 / 2) + 1) = 3
Now, the tree is traversed in the following way:
Middle level: 14, 15
One level above the middle level: 12, 13
One level below the middle level: 21, 22, 23, 24
Two levels above the middle level: 10
Input:
5
/ \
2 13
/ \ \
4 25 6
/ / \
11 3 21
\
9
Output:
4, 25, 6
2, 13
11, 3, 21
5
9
Explanation:
There are 5 levels in the tree.
Therefore, Middle level = ((5 / 2) + 1) = 3.
Now, the tree is traversed in the following way:
Middle level: 4, 25, 6
One level above the middle level: 2, 13
One level below the middle level: 11, 3, 21
Two levels above the middle level: 5
Two levels below the middle level: 9
Approach: The idea is to store the tree in a matrix. In order to do that,

- Then, simply iterate over the matrix and print the values from the matrix as per the given condition.
Below is the implementation of the above approach:
C++
// C++ program to traverse the tree
// from the middle to up and down
#include <bits/stdc++.h>
using namespace std;
// A Tree node
struct Node {
int key;
struct Node *left, *right;
};
// Utility function to create
// a new node
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// Function to calculate the
// height of the tree
int findHeight(struct Node* node)
{
// Base condition
if (node == NULL)
return 0;
int leftHeight = findHeight(node->left);
int rightHeight = findHeight(node->right);
// Return the maximum of the height
// of the left and right subtree
return 1 + (leftHeight > rightHeight
? leftHeight
: rightHeight);
}
// Function to find the width of the tree
void findWidth(struct Node* node, int& maxValue,
int& minValue, int hd)
{
// Base cases
if (node == NULL)
return;
if (hd > maxValue) {
maxValue = hd;
}
if (hd < minValue) {
minValue = hd;
}
// Recursively call the function twice to find
// the maximum width
findWidth(node->left, maxValue, minValue, hd - 1);
findWidth(node->right, maxValue, minValue, hd + 1);
}
// Function to traverse the tree and
// store level order traversal in a matrix
void BFS(int** mtrx, struct Node* node)
{
// Create queue for storing
// the addresses of nodes
queue<struct Node*> qu;
qu.push(node);
int i = -1, j = -1;
struct Node* poped_node = NULL;
// Iterating over the queue to perform
// breadth-first search traversal
while (!qu.empty()) {
i++;
int qsize = qu.size();
while (qsize--) {
j++;
poped_node = qu.front();
// Store the data of the node into
// the matrix
mtrx[i][j] = poped_node->key;
qu.pop();
// Performing BFS for the remaining
// nodes in the queue
if (poped_node->left != NULL) {
qu.push(poped_node->left);
}
if (poped_node->right != NULL) {
qu.push(poped_node->right);
}
}
j = -1;
}
}
// Function for Middle to Up Down
// Traversal of Binary Tree
void traverse_matrix(int** mtrx, int height,
int width)
{
// Variables to handle rows and columns
// of the matrix
int up = (height / 2);
int down = up + 1;
bool flag = true;
int k = 0;
// Print the middle row
for (int j = 0; j < width; j++) {
if (mtrx[up][j] != INT_MAX) {
cout << mtrx[up][j] << ", ";
}
}
cout << endl;
up--;
// Loop to print the remaining rows
for (int i = 0; i < (height - 1); i++) {
// Condition to manage up and
// down indices in the matrix
if (flag) {
k = up;
up--;
flag = !flag;
}
else {
k = down;
down++;
flag = !flag;
}
// Loop to print the value
// of matrix cells in particular row
for (int j = 0; j < width; j++) {
if (mtrx[k][j] != INT_MAX) {
cout << mtrx[k][j] << ", ";
}
}
cout << endl;
}
}
// A utility function for middle to
// up down traversal
void printPattern(struct Node* node)
{
// max, min has been taken for
// calculating the width of tree
int max_value = INT_MIN;
int min_value = INT_MAX;
int hd = 0;
// Calculate the width of a tree
findWidth(node, max_value, min_value, hd);
int width = max_value + abs(min_value);
// Calculate the height of the tree
int height = findHeight(node);
// Double pointer to create 2D array
int** mtrx = new int*[height];
// Initialize the width for
// each row of the matrix
for (int i = 0; i < height; i++) {
mtrx[i] = new int[width];
}
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
mtrx[i][j] = INT_MAX;
}
}
// Store the BFS traversal of the tree
// into the 2-D matrix
BFS(mtrx, node);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, height, width);
// release extra memory
// allocated for matrix
free(mtrx);
}
// Driver Code
int main()
{
/*
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
*/
// Creating the above tree
Node* root = newNode(10);
root->left = newNode(12);
root->right = newNode(13);
root->right->left = newNode(14);
root->right->right = newNode(15);
root->right->left->left = newNode(21);
root->right->left->right = newNode(22);
root->right->right->left = newNode(23);
root->right->right->right = newNode(24);
printPattern(root);
return 0;
}
Java
// Java program to traverse the tree
// from the middle to up and down
import java.util.*;
class GFG{
// A Tree node
static class Node {
int key;
Node left, right;
};
static int maxValue, minValue;
// Utility function to create
// a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Function to calculate the
// height of the tree
static int findHeight(Node node)
{
// Base condition
if (node == null)
return 0;
int leftHeight = findHeight(node.left);
int rightHeight = findHeight(node.right);
// Return the maximum of the height
// of the left and right subtree
return 1 + (leftHeight > rightHeight
? leftHeight
: rightHeight);
}
// Function to find the width of the tree
static void findWidth(Node node, int hd)
{
// Base cases
if (node == null)
return;
if (hd > maxValue) {
maxValue = hd;
}
if (hd < minValue) {
minValue = hd;
}
// Recursively call the function twice to find
// the maximum width
findWidth(node.left, hd - 1);
findWidth(node.right, hd + 1);
}
// Function to traverse the tree and
// store level order traversal in a matrix
static void BFS(int [][]mtrx, Node node)
{
// Create queue for storing
// the addresses of nodes
Queue<Node> qu = new LinkedList<Node>();
qu.add(node);
int i = -1, j = -1;
Node poped_node = null;
// Iterating over the queue to perform
// breadth-first search traversal
while (!qu.isEmpty()) {
i++;
int qsize = qu.size();
while (qsize-- > 0) {
j++;
poped_node = qu.peek();
// Store the data of the node into
// the matrix
mtrx[i][j] = poped_node.key;
qu.remove();
// Performing BFS for the remaining
// nodes in the queue
if (poped_node.left != null) {
qu.add(poped_node.left);
}
if (poped_node.right != null) {
qu.add(poped_node.right);
}
}
j = -1;
}
}
// Function for Middle to Up Down
// Traversal of Binary Tree
static void traverse_matrix(int [][]mtrx, int height,
int width)
{
// Variables to handle rows and columns
// of the matrix
int up = (height / 2);
int down = up + 1;
boolean flag = true;
int k = 0;
// Print the middle row
for (int j = 0; j < width; j++) {
if (mtrx[up][j] != Integer.MAX_VALUE) {
System.out.print(mtrx[up][j]+ ", ");
}
}
System.out.println();
up--;
// Loop to print the remaining rows
for (int i = 0; i < (height - 1); i++) {
// Condition to manage up and
// down indices in the matrix
if (flag) {
k = up;
up--;
flag = !flag;
}
else {
k = down;
down++;
flag = !flag;
}
// Loop to print the value
// of matrix cells in particular row
for (int j = 0; j < width; j++) {
if (mtrx[k][j] != Integer.MAX_VALUE) {
System.out.print(mtrx[k][j]+ ", ");
}
}
System.out.println();
}
}
// A utility function for middle to
// up down traversal
static void printPattern(Node node)
{
// max, min has been taken for
// calculating the width of tree
maxValue = Integer.MIN_VALUE;
minValue = Integer.MAX_VALUE;
int hd = 0;
// Calculate the width of a tree
findWidth(node, hd);
int width = maxValue + Math.abs(minValue);
// Calculate the height of the tree
int height = findHeight(node);
// Double pointer to create 2D array
int [][]mtrx = new int[width][height];
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
mtrx[i][j] = Integer.MAX_VALUE;
}
}
// Store the BFS traversal of the tree
// into the 2-D matrix
BFS(mtrx, node);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, height, width);
// release extra memory
// allocated for matrix
mtrx =null;
}
// Driver Code
public static void main(String[] args)
{
/*
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
*/
// Creating the above tree
Node root = newNode(10);
root.left = newNode(12);
root.right = newNode(13);
root.right.left = newNode(14);
root.right.right = newNode(15);
root.right.left.left = newNode(21);
root.right.left.right = newNode(22);
root.right.right.left = newNode(23);
root.right.right.right = newNode(24);
printPattern(root);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the
# above approach
from collections import deque
# A Tree node
class Node:
def __init__(self, x):
self.key = x
self.left = None
self.right = None
minValue, maxValue = 0, 0
# Function to calculate the
# height of the tree
def findHeight(node):
# Base condition
if (node == None):
return 0
leftHeight = findHeight(node.left)
rightHeight = findHeight(node.right)
# Return the maximum of the height
# of the left and right subtree
if leftHeight > rightHeight:
return leftHeight + 1
return rightHeight + 1
# Function to find the width
# of the tree
def findWidth(node, hd):
global minValue, maxValue
# Base cases
if (node == None):
return
if (hd > maxValue):
maxValue = hd
if (hd < minValue):
minValue = hd
# Recursively call the
# function twice to find
# the maximum width
findWidth(node.left,
hd - 1)
findWidth(node.right,
hd + 1)
# Function to traverse the
# tree and store level order
# traversal in a matrix
def BFS(mtrx, node):
# Create queue for storing
# the addresses of nodes
qu = deque()
qu.append(node)
i = -1
j = -1
poped_node = None
# Iterating over the queue
# to perform breadth-first
# search traversal
while (len(qu) > 0):
i += 1
qsize = len(qu)
while (qsize):
j += 1
poped_node = qu.popleft()
# Store the data of the
# node into the matrix
mtrx[i][j] = poped_node.key
#qu.pop()
# Performing BFS for
# the remaining nodes
# in the queue
if (poped_node.left != None):
qu.append(poped_node.left)
if (poped_node.right != None):
qu.append(poped_node.right)
qsize -= 1
j = -1
#Function for Middle to Up Down
#Traversal of Binary Tree
def traverse_matrix(mtrx,
height,width):
# Variables to handle rows
# and columns of the matrix
up = (height // 2)
down = up + 1
flag = True
k = 0
# Print middle row
for j in range(width):
if (mtrx[up][j] != 10 ** 9):
print(mtrx[up][j], end = ", ")
print()
up -= 1
# Loop to print the remaining rows
for i in range(height - 1):
# Condition to manage up and
# down indices in the matrix
if (flag):
k = up
up -= 1
flag = not flag
else:
k = down
down += 1
flag = not flag
# Loop to print the value
# of matrix cells in
# particular row
for j in range(width):
if (mtrx[k][j] != 10 ** 9):
print(mtrx[k][j], end = ", ")
print()
# A utility function for middle to
# up down traversal
def printPattern(node):
global maxValue, minValue
# max, min has been taken for
# calculating the width of tree
maxValue = -10 ** 9
minValue = 10 ** 9
hd = 0
# Calculate the width
# of a tree
findWidth(node, hd)
width = maxValue + abs(minValue)
# Calculate the height of the tree
height = findHeight(node)
# print(height,width)
#Double pointer to create 2D array
mtrx = [[10 ** 9 for i in range(width + 1)]
for i in range(height + 1)]
# Store the BFS traversal of the tree
# into the 2-D matrix
BFS(mtrx, node)
# Print circular clockwise spiral
# traversal of the tree
traverse_matrix(mtrx, height, width)
# Driver Code
if __name__ == '__main__':
# /*
# 10
# / \
# 12 13
# / \
# 14 15
# / \ / \
# 21 22 23 24
# */
# Creating the above tree
root = Node(10)
root.left = Node(12)
root.right = Node(13)
root.right.left = Node(14)
root.right.right = Node(15)
root.right.left.left = Node(21)
root.right.left.right = Node(22)
root.right.right.left = Node(23)
root.right.right.right = Node(24)
printPattern(root)
# This code is contributed by Mohit Kumar 29
C#
// C# program to traverse the tree
// from the middle to up and down
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// A Tree node
class Node
{
public int key;
public Node left, right;
};
static int maxValue, minValue;
// Utility function to create
// a new node
static Node newNode(int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null;
return (temp);
}
// Function to calculate the
// height of the tree
static int findHeight(Node node)
{
// Base condition
if (node == null)
return 0;
int leftHeight = findHeight(node.left);
int rightHeight = findHeight(node.right);
// Return the maximum of the height
// of the left and right subtree
return 1 + (leftHeight > rightHeight
? leftHeight
: rightHeight);
}
// Function to find the width of the tree
static void findWidth(Node node, int hd)
{
// Base cases
if (node == null)
return;
if (hd > maxValue) {
maxValue = hd;
}
if (hd < minValue) {
minValue = hd;
}
// Recursively call the function twice to find
// the maximum width
findWidth(node.left, hd - 1);
findWidth(node.right, hd + 1);
}
// Function to traverse the tree and
// store level order traversal in a matrix
static void BFS(int [,]mtrx, Node node)
{
// Create queue for storing
// the addresses of nodes
Queue qu = new Queue();
qu.Enqueue(node);
int i = -1, j = -1;
Node poped_node = null;
// Iterating over the queue to perform
// breadth-first search traversal
while (qu.Count != 0)
{
i++;
int qsize = qu.Count;
while (qsize-- > 0) {
j++;
poped_node = (Node)qu.Peek();
// Store the data of the node into
// the matrix
mtrx[i,j] = poped_node.key;
qu.Dequeue();
// Performing BFS for the remaining
// nodes in the queue
if (poped_node.left != null) {
qu.Enqueue(poped_node.left);
}
if (poped_node.right != null) {
qu.Enqueue(poped_node.right);
}
}
j = -1;
}
}
// Function for Middle to Up Down
// Traversal of Binary Tree
static void traverse_matrix(int [,]mtrx, int height,
int width)
{
// Variables to handle rows and columns
// of the matrix
int up = (height / 2);
int down = up + 1;
bool flag = true;
int k = 0;
// Print the middle row
for (int j = 0; j < width; j++) {
if (mtrx[up, j] != 100000000) {
Console.Write(mtrx[up,j]+ ", ");
}
}
Console.WriteLine();
up--;
// Loop to print the remaining rows
for (int i = 0; i < (height - 1); i++)
{
// Condition to manage up and
// down indices in the matrix
if (flag) {
k = up;
up--;
flag = !flag;
}
else {
k = down;
down++;
flag = !flag;
}
// Loop to print the value
// of matrix cells in particular row
for (int j = 0; j < width; j++)
{
if (mtrx[k, j] != 100000000) {
Console.Write(mtrx[k,j]+ ", ");
}
}
Console.WriteLine();
}
}
// A utility function for middle to
// up down traversal
static void printPattern(Node node)
{
// max, min has been taken for
// calculating the width of tree
maxValue = -100000000;
minValue = 100000000;
int hd = 0;
// Calculate the width of a tree
findWidth(node, hd);
int width = maxValue + Math.Abs(minValue);
// Calculate the height of the tree
int height = findHeight(node);
// Double pointer to create 2D array
int [,]mtrx = new int[width,height];
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
mtrx[i, j] = 100000000;
}
}
// Store the BFS traversal of the tree
// into the 2-D matrix
BFS(mtrx, node);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, height, width);
// release extra memory
// allocated for matrix
mtrx = null;
}
// Driver Code
public static void Main(string[] args)
{
/*
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
*/
// Creating the above tree
Node root = newNode(10);
root.left = newNode(12);
root.right = newNode(13);
root.right.left = newNode(14);
root.right.right = newNode(15);
root.right.left.left = newNode(21);
root.right.left.right = newNode(22);
root.right.right.left = newNode(23);
root.right.right.right = newNode(24);
printPattern(root);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// Javascript program to traverse the tree
// from the middle to up and down
// A Tree node
class Node
{
// Utility function to create
// a new node
constructor(key)
{
this.key = key;
this.left = this.right = null;
}
}
// Function to calculate the
// height of the tree
function findHeight(node)
{
// Base condition
if (node == null)
return 0;
let leftHeight = findHeight(node.left);
let rightHeight = findHeight(node.right);
// Return the maximum of the height
// of the left and right subtree
return 1 + (leftHeight > rightHeight ?
leftHeight : rightHeight);
}
// Function to find the width of the tree
function findWidth(node,hd)
{
// Base cases
if (node == null)
return;
if (hd > maxValue)
{
maxValue = hd;
}
if (hd < minValue)
{
minValue = hd;
}
// Recursively call the function twice
// to find the maximum width
findWidth(node.left, hd - 1);
findWidth(node.right, hd + 1);
}
// Function to traverse the tree and
// store level order traversal in a matrix
function BFS(mtrx, node)
{
// Create queue for storing
// the addresses of nodes
let qu = [];
qu.push(node);
let i = -1, j = -1;
let poped_node = null;
// Iterating over the queue to perform
// breadth-first search traversal
while (qu.length != 0)
{
i++;
let qsize = qu.length;
while (qsize-- > 0)
{
j++;
poped_node = qu[0];
// Store the data of the node into
// the matrix
mtrx[i][j] = poped_node.key;
qu.shift();
// Performing BFS for the remaining
// nodes in the queue
if (poped_node.left != null)
{
qu.push(poped_node.left);
}
if (poped_node.right != null)
{
qu.push(poped_node.right);
}
}
j = -1;
}
}
// Function for Middle to Up Down
// Traversal of Binary Tree
function traverse_matrix(mtrx, height, width)
{
// Variables to handle rows and columns
// of the matrix
let up = Math.floor(height / 2);
let down = up + 1;
let flag = true;
let k = 0;
// Print the middle row
for(let j = 0; j < width; j++)
{
if (mtrx[up][j] != Number.MAX_VALUE)
{
document.write(mtrx[up][j] + ", ");
}
}
document.write("<br>");
up--;
// Loop to print the remaining rows
for(let i = 0; i < (height - 1); i++)
{
// Condition to manage up and
// down indices in the matrix
if (flag)
{
k = up;
up--;
flag = !flag;
}
else
{
k = down;
down++;
flag = !flag;
}
// Loop to print the value
// of matrix cells in particular row
for(let j = 0; j < width; j++)
{
if (mtrx[k][j] != Number.MAX_VALUE)
{
document.write(mtrx[k][j] + ", ");
}
}
document.write("<br>");
}
}
// A utility function for middle to
// up down traversal
function printPattern(node)
{
// max, min has been taken for
// calculating the width of tree
maxValue = Number.MIN_VALUE;
minValue = Number.MAX_VALUE;
let hd = 0;
// Calculate the width of a tree
findWidth(node, hd);
let width = maxValue + Math.abs(minValue);
// Calculate the height of the tree
let height = findHeight(node);
// Double pointer to create 2D array
let mtrx = new Array(width);
for(let i = 0; i < width; i++)
{
mtrx[i] = new Array(height);
}
// Initialize complete matrix with
// MAX INTEGER(purpose garbage)
for(let i = 0; i < height; i++)
{
for(let j = 0; j < width; j++)
{
mtrx[i][j] = Number.MAX_VALUE;
}
}
// Store the BFS traversal of the tree
// into the 2-D matrix
BFS(mtrx, node);
// Print the circular clockwise spiral
// traversal of the tree
traverse_matrix(mtrx, height, width);
// release extra memory
// allocated for matrix
mtrx = null;
}
// Driver Code
/*
10
/ \
12 13
/ \
14 15
/ \ / \
21 22 23 24
*/
// Creating the above tree
let root = new Node(10);
root.left = new Node(12);
root.right = new Node(13);
root.right.left = new Node(14);
root.right.right = new Node(15);
root.right.left.left = new Node(21);
root.right.left.right = new Node(22);
root.right.right.left = new Node(23);
root.right.right.right = new Node(24);
printPattern(root);
// This code is contributed by unknown2108
</script>
Output: 14, 15,
12, 13,
21, 22, 23, 24,
10,
The time complexity of the findHeight function is O(V), as it visits each node in the tree exactly once and performs a constant amount of work for each node. The time complexity of the findWidth function is also O(V), as it similarly visits each node in the tree exactly once
The Auxiliary Space of this implementation is O(V), where V is the number of nodes in the tree. This is because the BFS function uses a queue to store the addresses of the nodes during the traversal, and the size of the queue is at most equal to the number of nodes in the tree.
Similar Reads
Double Order Traversal of a Binary Tree
Given a Binary Tree, the task is to find its Double Order Traversal. Double Order Traversal is a tree traversal technique in which every node is traversed twice in the following order: Visit the Node.Traverse the Left Subtree.Visit the Node.Traverse the Right Subtree.Examples:Input: Output: 1 7 4 4
6 min read
Mix Order Traversal of a Binary Tree
Given a Binary Tree consisting of N nodes, the task is to print its Mix Order Traversal. Mix Order Traversal is a tree traversal technique, which involves any two of the existing traversal techniques like Inorder, Preorder and Postorder Traversal. Any two of them can be performed or alternate levels
13 min read
Boundary Level order traversal of a Binary Tree
Given a Binary Tree, the task is to print all levels of this tree in Boundary Level order traversal. Boundary Level order traversal: In this traversal, the first element of the level (starting boundary) is printed first, followed by last element (ending boundary). Then the process is repeated for th
11 min read
Find n-th node in Preorder traversal of a Binary Tree
Given a binary tree. The task is to find the n-th node of preorder traversal.Examples: Input:Output: 50Explanation: Preorder Traversal is: 10 20 40 50 30 and value of 4th node is 50.Input:Output : 3Explanation: Preorder Traversal is: 7 2 3 8 5 and value of 3rd node is 3.Table of Content[Naive Approa
11 min read
Triple Order Traversal of a Binary Tree
Given a Binary Tree, the task is to find its Triple Order Traversal. Triple Order Traversal is a tree traversal technique in which every node is traversed thrice in the following order: Visit the root nodeTraverse the left subtreeVisit the root nodeTraverse the right subtreeVisit the root node.Examp
7 min read
Flatten Binary Tree in order of Level Order Traversal
Given a Binary Tree, the task is to flatten it in order of Level order traversal of the tree. In the flattened binary tree, the left node of all the nodes must be NULL.Examples: Input: 1 / \ 5 2 / \ / \ 6 4 9 3 Output: 1 5 2 6 4 9 3 Input: 1 \ 2 \ 3 \ 4 \ 5 Output: 1 2 3 4 5 Approach: We will solve
7 min read
Inorder Traversal of Binary Tree
Inorder traversal is a depth-first traversal method that follows this sequence:Left subtree is visited first.Root node is processed next.Right subtree is visited last.How does Inorder Traversal work?Key Properties:If applied to a Binary Search Tree (BST), it returns elements in sorted order.Ensures
5 min read
Find the kth node in vertical order traversal of a Binary Tree
Given a binary tree and an integer k, the task is to return the kth node in the vertical order traversal of a binary tree. If no such node exists then return -1.The vertical order traversal of a binary tree means to print it vertically.Examples: Input: k = 3Output: 1Explanation: The below image show
9 min read
Bottom-left to upward-right Traversal in a Binary Tree
Given a Binary Tree, the task is to print the Bottom-left to Upward-right Traversal of the given Binary Tree i.e., the level order traversal having level as Bottom-left to Upward-right node. Examples: Input: Below is the given Tree: Output: 2 7 2 5 6 5 11 4 9Explanation: Level 1: 2 7 2 (going upward
15 min read
Boundary Root to Leaf Path traversal of a Binary Tree
Given a Binary Tree, the task is to print all Root to Leaf path of this tree in Boundary Root to Leaf path traversal. Boundary Root to Leaf Path Traversal: In this traversal, the first Root to Leaf path(Left boundary) is printed first, followed by the last Root to Leaf path (Right boundary) in Rever
15+ min read