Google a yazarsanız geeksforgeeks'teki link açıklıyor iki farklı yöntemle.
Recursive olanı tercih ederim ben