A Tree is a generalization of connected graph where it has N nodes that will have exactly N-1 edges, i.e one edge between every pair of vertices. Find the Euler tour of tree represented by adjacency list.
Examples:
Input :

Output : 1 2 3 2 4 2 1
Input :

Output : 1 5 4 2 4 3 4 5 1
Euler tour is defined as a way of traversing tree such that each vertex is added to the tour when we visit it (either moving down from parent vertex or returning from child vertex). We start from root and reach back to root after visiting all vertices.
It requires exactly 2*N-1 vertices to store Euler tour.
Approach: We will run DFS(Depth first search) algorithm on Tree as:

- Visit root node, i.e 1
vis[1]=1, Euler[0]=1
run dfs() for all unvisited adjacent nodes(2)
- Visit node 2
vis[2]=1, Euler[1]=2
run dfs() for all unvisited adjacent nodes(3, 4)
- Visit node 3
vis[3]=1, Euler[2]=3
All adjacent nodes are already visited, return to parent node
and add parent to Euler tour Euler[3]=2
- Visit node 4
vis[4]=1, Euler[4]=4
All adjacent nodes are already visited, return to parent node
and add parent to Euler tour, Euler[5]=2
- Visit node 2
All adjacent nodes are already visited, return to parent node
and add parent to Euler tour, Euler[6]=1
- Visit node 1
All adjacent nodes are already visited, and node 1 is root node
so, we stop our recursion here.
Similarly, for example 2:

Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1001
vector<int> adj[MAX];
int vis[MAX];
int Euler[2 * MAX];
void add_edge(int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void eulerTree(int u, int &index;)
{
vis[u] = 1;
Euler[index++] = u;
for (auto it : adj[u]) {
if (!vis[it]) {
eulerTree(it, index);
Euler[index++] = u;
}
}
}
void printEulerTour(int root, int N)
{
int index = 0;
eulerTree(root, index);
for (int i = 0; i < (2*N-1); i++)
cout << Euler[i] << " ";
}
int main()
{
int N = 4;
add_edge(1, 2);
add_edge(2, 3);
add_edge(2, 4);
printEulerTour(1, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int MAX = 1001;
static int index = 0;
static ArrayList<
ArrayList<Integer>> adj = new ArrayList<>();
static int vis[] = new int[MAX];
static int Euler[] = new int[2 * MAX];
static void add_edge(int u, int v)
{
adj.get(u).add(v);
adj.get(v).add(u);
}
static void eulerTree(int u)
{
vis[u] = 1;
Euler[index++] = u;
for(int it : adj.get(u))
{
if (vis[it] == 0)
{
eulerTree(it);
Euler[index++] = u;
}
}
}
static void printEulerTour(int root, int N)
{
eulerTree(root);
for(int i = 0; i < (2 * N - 1); i++)
System.out.print(Euler[i] + " ");
}
public static void main(String[] args)
{
int N = 4;
for(int i = 0; i <= N; i++)
adj.add(new ArrayList<>());
add_edge(1, 2);
add_edge(2, 3);
add_edge(2, 4);
printEulerTour(1, N);
}
}
|
Python3
from collections import defaultdict
adj = defaultdict(list)
vis = defaultdict(bool)
MAX = 1001
Euler = [0]*(2*MAX)
def add_edge(u, v):
adj[u].append(v)
adj[v].append(u)
def eulerTree(u, index):
vis[u] = True
Euler[index] = u
index += 1
for nbr in adj[u]:
if not vis[nbr]:
index = eulerTree(nbr, index)
Euler[index] = u
index += 1
return index
def printEulerTour(root, N):
index = 0
eulerTree(root, index)
for i in range(2*N-1):
print(Euler[i], end=" ")
N = 4
add_edge(1, 2)
add_edge(2, 3)
add_edge(2, 4)
printEulerTour(1, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
const int MAX = 1001;
static int index = 0;
static List<List<int>> adj = new List<List<int>>();
static int[] vis = new int[MAX];
static int[] Euler = new int[2 * MAX];
static void add_edge(int u, int v)
{
adj[u].Add(v);
adj[v].Add(u);
}
static void eulerTree(int u)
{
vis[u] = 1;
Euler[index++] = u;
foreach (int it in adj[u])
{
if (vis[it] == 0)
{
eulerTree(it);
Euler[index++] = u;
}
}
}
static void printEulerTour(int root, int N)
{
eulerTree(root);
for (int i = 0; i < (2 * N - 1); i++)
Console.Write(Euler[i] + " ");
}
public static void Main(string[] args)
{
int N = 4;
for (int i = 0; i <= N; i++)
adj.Add(new List<int>());
add_edge(1, 2);
add_edge(2, 3);
add_edge(2, 4);
printEulerTour(1, N);
}
}
|
Javascript
<script>
var MAX = 1001;
var adj = Array.from(Array(MAX), () => Array());
var vis = Array(MAX);
var Euler = Array(2 * MAX);
function add_edge(u, v)
{
adj[u].push(v);
adj[v].push(u);
}
function eulerTree(u, index)
{
vis[u] = 1;
Euler[index++] = u;
for(var it of adj[u])
{
if (!vis[it])
{
index = eulerTree(it, index);
Euler[index++] = u;
}
}
return index;
}
function printEulerTour(root, N)
{
var index = 0;
index = eulerTree(root, index);
for(var i = 0; i < (2 * N - 1); i++)
document.write(Euler[i] + " ");
}
var N = 4;
add_edge(1, 2);
add_edge(2, 3);
add_edge(2, 4);
printEulerTour(1, N);
</script>
|
Complexity Analysis:
- Auxiliary Space: O(N)
- Time Complexity: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...