Implement Recursive Algorithms with Sub-workflows: Towers of Hanoi Demo
How it works
This is an example of using sub-workflow nodes and a proof of concept showing that it’s possible to solve and explain recursive problems with n8n.
Towers of Hanoi - Task Move a stack of n disks from rod A to rod C, using rod B as auxiliary. Only one disk can be moved at a time, and no disk may be placed on a smaller disk.
Example n=4 | | | = | | === | | ===== | | ======= | |
A B C
Algorithm procedure Hanoi(n, X, Y, Z): if n == 1: move disk from X to Z else: Hanoi(n-1, X, Z, Y) move disk from X to Z Hanoi(n-1, Y, X, Z)
Notes This is a learning example. In a real scenario, you would probably use an iterative approach with only a single code node. When experimenting with recursion, make sure to define a termination condition first. Also, be aware of the "Restart workspace" link in the n8n Dashboard. Learn more about Recursion on Wikipedia&oldid=1301600240#Towers_of_Hanoi).
Set up steps Optional: Set "numberOfDiscs" in node "Set number of discs" Execute workflow
Related Templates
AI SEO Readability Audit: Check Website Friendliness for LLMs
Who is this for? This workflow is designed for SEO specialists, content creators, marketers, and website developers who ...
Use OpenRouter in n8n versions <1.78
What it is: In version 1.78, n8n introduced a dedicated node to use the OpenRouter service, which lets you to use a lot...
Task Deadline Reminders with Google Sheets, ChatGPT, and Gmail
Intro This template is for project managers, team leads, or anyone who wants to automatically remind teammates of tasks ...
🔒 Please log in to import templates to n8n and favorite templates
Workflow Visualization
Loading...
Preparing workflow renderer
Comments (0)
Login to post comments