Euler Path and Circuit - Problem
An Euler path is a path in a graph that visits every edge exactly once. An Euler circuit is an Euler path that starts and ends at the same vertex.
Given a graph represented as an adjacency list, determine if the graph has an Euler path or circuit, and if so, return the path. Use Hierholzer's algorithm to find the path efficiently.
The graph can be either directed or undirected. For undirected graphs, an Euler path exists if and only if the graph is connected and has exactly 0 or 2 vertices with odd degree. For directed graphs, an Euler path exists if the graph is weakly connected and has at most one vertex with out-degree - in-degree = 1, at most one vertex with in-degree - out-degree = 1, and all other vertices have equal in-degree and out-degree.
Return the Euler path/circuit as an array of vertices, or an empty array if no such path exists.
Input & Output
Constraints
- 1 ≤ graph.length ≤ 1000
- 0 ≤ graph[i].length ≤ 1000
- 0 ≤ graph[i][j] < graph.length
- isDirected is either true or false