ExamAdda Logo

Reconstruct Itinerary

Easy

You are given a list of airline tickets where:

tickets[i] = [from, to]

 

Each ticket represents a flight from one airport to another.

All tickets belong to one traveler who starts at:

JFK

 

You must reconstruct the complete itinerary.

Rules

  • The itinerary must start from JFK.
  • Every ticket must be used exactly once.
  • If multiple valid itineraries are possible, return the one with the smallest lexical order.

Example

tickets =

[

    ["MUC","LHR"],

    ["JFK","MUC"],

    ["SFO","SJC"],

    ["LHR","SFO"]

]

 

The itinerary is:

JFK → MUC → LHR → SFO → SJC

 

So the answer is:

["JFK","MUC","LHR","SFO","SJC"]

 

Main Idea

This is a Graph + DFS + Eulerian Path problem.

Think of:

Airport → Node

Ticket  → Directed Edge

 

For example:

JFK → MUC

MUC → LHR

LHR → SFO

SFO → SJC

 

The important part is that every ticket must be used exactly once.

So this is an Eulerian Path problem.

Approach

  1. Build an adjacency list.
  2. Sort destinations lexicographically.
  3. Start DFS from JFK.
  4. Always choose the smallest available destination.
  5. Remove a ticket when using it.
  6. If an airport has no unused tickets left, add it to the answer.
  7. Reverse the resulting route.

Why postorder?

Consider:

JFK → A

JFK → B

A → JFK

 

Simply choosing the smallest destination greedily can lead to a dead end.

DFS handles this by adding an airport to the result only after all its outgoing tickets have been used.

This is the key idea behind Hierholzer's Algorithm.

Example 1

Input

4
MUC LHR
JFK MUC
SFO SJC
LHR SFO

Output

JFK MUC LHR SFO SJC

Explanation

The tickets form:

JFK → MUC → LHR → SFO → SJC

 

Every ticket is used exactly once.

Therefore:

["JFK","MUC","LHR","SFO","SJC"]

 

is the required itinerary.

Example 2

Input

5
JFK SFO
JFK ATL
SFO ATL
ATL JFK
ATL SFO

Output

JFK ATL JFK SFO ATL SFO

Explanation

There are multiple possible routes.

The lexicographically smaller valid itinerary is:

JFK

 ↓

ATL

 ↓

JFK

 ↓

SFO

 ↓

ATL

 ↓

SFO

 

Therefore:

["JFK","ATL","JFK","SFO","ATL","SFO"]

 

is returned.

Constraints

1 <= tickets.length <= 300
tickets[i].length == 2
fromi.length == 3
toi.length == 3
fromi and toi consist of uppercase English letters.
fromi != toi
All tickets form at least one valid itinerary.

Hints:

Hint 1

Think of each airport as a node and each ticket as a directed edge.

Hint 2

Since every ticket must be used exactly once, this is an Eulerian Path problem.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 16, 2026

Last updated

Aug 17, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output