There was a discussion in comments on Stackoverflow in Russian about detecting palindrome.
I give such an example for checking the linked list for palindrome (it’s leetcode #234 Palindrome Linked List):
public class Solution
public bool IsPalindrome(ListNode head)
Using math for solving many programming problems is the fastest way.
For example, you can solve leetcode #62 Unique Paths mathematically. A result is number of n — 1 combinations from m — 1 + n — 1:
Today when I repeated the solution for leetcode #876 Middle of the Linked List, I thought suddenly. What if we need to return the first middle element, not the second?
Let me remind you. The list can contain an odd number of elements:
Input: head = [1,2,3,4,5]
Explanation: The middle…
I’ve found an ATM analogy answering what recursion is in this video.
This analogy may help you to solve leetcode #104 Maximum Depth of Binary Tree. All you need is to imagine that you have multiple ATMs and need to choose a max path:
public int MaxDepth(TreeNode root)
if (root == null)
var left = 1 + MaxDepth(root.left);
var right = 1 + MaxDepth(root.right);
return Math.Max(left, right);
As for me, it’s a simple analogy helping to remember the recursive approach for solving this problem.
Two days ago I tried to solve again leetcode problem 0119 Pascal’s triangle using the math approach. It is the fastest approach to calculate row in Pascal’s triangle, but… I can’t understand how it works and how to remember the formulas.
So, let’s begin from the base.
Traverse binary tree iteratively is hard to remember, especially for postorder.
Just as an illustration.
public IList<int> PreorderTraversal(TreeNode root)
var result = new List<int>();
var stack = new Stack<TreeNode>();
while (stack.Count != 0 || root != null)…
I often see that candidates use one dictionary when solving leetcode problem #205 ‘Isomorphic Strings’.
There are a lot of submissions in leetcode discussions, they are something like this:
public bool IsIsomorphic(string s, string t)
var dict = new Dictionary<char,char>()…