DEV Community

Cover image for Learn Valid Parentheses, Reverse Linked List, and Tree Max Depth with Step-by-Step Visualization in DSA View View 👀👀
nyaomaru
nyaomaru

Posted on Edited on AI-assisted

Learn Valid Parentheses, Reverse Linked List, and Tree Max Depth with Step-by-Step Visualization in DSA View View 👀👀

Curated progression from stacks to recursion

Hoi hoi!

I’m @nyaomaru, a frontend engineer who struggles to make game sounds. 😿

Have you used DSA View View already? 👀👀

DSA View View allows you to understand DSA by visualizing how your implementation actually runs.

In the previous article, we looked at:

  • Two Sum
  • Binary Search
  • Bubble Sort

This time, let's try three more classic problems:

  • Valid Parentheses
  • Reverse Linked List
  • Maximum Depth of Binary Tree

These problems introduce three very different ideas:

Stack
Pointer manipulation
Recursion
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And all three can become confusing when we only stare at the final code.

So let's view what actually happens. 👀

I'm still learning DSA too, so let's learn together! 😸


🥞 Valid Parentheses

Let's start with Valid Parentheses.

Suppose we have this string:

()[]{}
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Every opening bracket has a matching closing bracket.

So this is valid. ✅

But this

([)]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

is not valid. ❌

Why?

Because the brackets close in the wrong order.

(
  [
)
  ]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The [ should be closed before the (.

So how can we keep track of that order?

Use a Stack

A stack follows a simple rule:

The last thing we put in is the first thing we take out.

This is called LIFO (Last In / First Out)

Imagine stacking plates.

    🍽️  ← remove first
    🍽️
    🍽️
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The last plate we put on top is the first one we can take.

Parentheses work in the same way.

If we see:

(
[
{
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

then the brackets must close in reverse order

}
]
)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So a stack is a very natural fit.

Here is the implementation:

function isValid(s: string): boolean {
  const stack: string[] = [];

  const pairs: Record<string, string> = {
    ")": "(",
    "]": "[",
    "}": "{",
  };

  for (const char of s) {
    if (char === "(" || char === "[" || char === "{") {
      stack.push(char);
      continue;
    }

    if (stack.length === 0) return false;

    const target = stack.pop();
    if (target !== pairs[char]) {
      return false;
    }
  }

  return stack.length === 0;
}
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The important part is what happens to stack.

Let's use

([])
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

At first

stack = []
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

We see

(
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

It's an opening bracket.

Push it.

stack = ["("]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Next,

[
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Push again.

stack = ["(", "["]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Then,

]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

This is a closing bracket.

What should it close?

[
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And what is currently on top of the stack?

[
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Perfect. ✅

So we remove it.

stack = ["("]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Finally,

)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

It should close

(
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The top of the stack is also

(
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Pop!

stack = []
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

We reached the end with an empty stack.

Valid! 🎉

What About an Invalid Example?

Consider

([)]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

We start the same way.

(
↓
stack = ["("]

[
↓
stack = ["(", "["]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Then we find

)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

A ) needs

(
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

But the top of our stack is

[
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

They don't match.

expected: (
actual:   [
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So we immediately know the string is invalid.

Complexity

We walk through the string once.

Time:  O(n)
Space: O(n)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

In the worst case, the stack may contain all opening brackets.

👀 Let's View View It

This is where the stack becomes much easier to understand.

When we only read.

stack.push(char);
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And

stack.pop();
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

It can be easy to lose track of what is actually inside the stack.

Especially with something like.

({[]})
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

What is on top right now?

Which opening bracket are we trying to close?

Instead of remembering everything in our head, we can follow the stack changing step by step.

Conceptually, we can see:

(
↓
[(]

{
↓
[(, {]

[
↓
[(, {, []

]
↓
[(, {]

}
↓
[(]

)
↓
[]
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

That's the whole idea.

Remember the opening brackets, and always match the most recent one first.

Stack suddenly feels much less mysterious. 🥞😸


🔗 Reverse Linked List

Next, let's reverse a linked list.

Suppose we have

1 → 2 → 3 → 4 → 5
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

We want

5 → 4 → 3 → 2 → 1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

At first glance, this sounds simple.

Just reverse it!

But linked lists are a little different from arrays.

With an array, the values live in positions like

0  1  2  3  4
↓  ↓  ↓  ↓  ↓
1  2  3  4  5
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

A linked list instead consists of nodes pointing to the next node.

1 → 2 → 3 → 4 → 5 → null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Each arrow matters. To reverse the list, we need to reverse those arrows.

1 ← 2 ← 3 ← 4 ← 5
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And this is where things can get confusing.

Because if we change an arrow too early...

we may lose the rest of the list. 😿

Three Important Variables

A common iterative solution uses three variables

prev
current
next
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Here is the implementation:

function reverseList(head: ListNode | null): ListNode | null {
  let prev: ListNode | null = null;
  let current = head;

  while (current !== null) {
    const next = current.next;

    current.next = prev;

    prev = current;
    current = next;
  }

  return prev;
}
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

It's short.

But there is a lot happening inside these few lines.

Let's follow it carefully.

We start with

1 → 2 → 3 → null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And

prev = null
current = 1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Step 1: Save the Next Node

First

const next = current.next;
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So

next = 2
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Why do we need this?

Because we're about to change

1 → 2
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

If we change that arrow without remembering 2, we lose access to the rest of the list.

So first

Save where we need to go next.

Step 2: Reverse the Arrow

Now

current.next = prev;
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Originally

1 → 2
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

But prev is

null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So now

1 → null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The first arrow has been reversed.

Step 3: Move prev

Next

prev = current;
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So

prev = 1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Step 4: Move current

Finally

current = next;
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

We saved 2 earlier.

So now

current = 2
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Our state looks like this

null ← 1    2 → 3 → null
       ↑    ↑
      prev current
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Then we do exactly the same thing again.

Save:

next = 3
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Reverse

2 → 1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Move

prev = 2
current = 3
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Now

null ← 1 ← 2    3 → null
           ↑    ↑
          prev  current
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

One more time

next = null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Reverse

3 → 2
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Move

prev = 3
current = null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And now

null ← 1 ← 2 ← 3
               ↑
              prev
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The loop stops because

current === null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And prev is the new head.

So

return prev;
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Done! 🎉

Complexity

We visit each node once.

Time:  O(n)
Space: O(1)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

We don't create another linked list.

We only move a few pointers.

👀 Let's View View It

This is exactly the kind of code that I find difficult to understand by reading alone.

These four lines:

const next = current.next;
current.next = prev;
prev = current;
current = next;
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

look simple.

But when I first see code like this, my brain starts asking.

Wait.

- Where is current now?
- Did we lose next?
- Which arrow changed?
- What exactly does prev point to?
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

😿

When we visualize each step, we can actually follow the pointers moving.

prev      current
 ↓           ↓
null         1 → 2 → 3

      ↓↓↓

null ← 1     2 → 3
       ↑     ↑
      prev current

      ↓↓↓

null ← 1 ← 2     3
           ↑     ↑
          prev current

      ↓↓↓

null ← 1 ← 2 ← 3
               ↑
              prev
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The algorithm becomes much simpler when we stop thinking of it as four mysterious assignments.

It's really just

Save next
  ↓
Reverse arrow
  ↓
Move prev
  ↓
Move current
  ↓
Repeat
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Nice! 🔗😸


🌳 Maximum Depth of Binary Tree

Finally, let's look at a tree.

Consider this binary tree.

        3
       / \
      9   20
         /  \
        15   7
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

What is its maximum depth?

The longest path from the root to a leaf contains three nodes:

3
↓
20
↓
15
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So the answer is.

3
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

How can we calculate that?

Think About a Smaller Tree

Suppose we are standing at one node.

We don't really need to understand the entire tree at once.

We only need to ask.

How deep is the left subtree?

How deep is the right subtree?
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Then choose the larger one.

And add 1 for the current node.

That's exactly what this implementation does.

function maxDepth(root: TreeNode | null): number {
  if (root === null) {
    return 0;
  }

  const leftDepth = maxDepth(root.left);
  const rightDepth = maxDepth(root.right);

  return Math.max(leftDepth, rightDepth) + 1;
}
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The important idea is

Math.max(leftDepth, rightDepth) + 1;
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

But recursion can feel strange.

When we call

maxDepth(root.left);
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

where does the current function go?

And how do all those calls eventually become one number?

Let's follow a small example.

    1
   / \
  2   3
 /
4
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

We start at

1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

But before 1 can know its depth, it asks its left child

maxDepth(2)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Node 2 asks

maxDepth(4)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Node 4 has no children.

So both sides eventually reach

null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And

maxDepth(null);
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

returns

0
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Therefore node 4 can calculate

max(0, 0) + 1
= 1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Now we return to node 2.

Its left side has depth

1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Its right side is null

0
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So

max(1, 0) + 1
= 2
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Now return to node 1.

Eventually its right subtree also returns

1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So node 1 gets

leftDepth = 2
rightDepth = 1
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And calculates

max(2, 1) + 1
= 3
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Answer

3
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

🎉

The Interesting Part: Going Down and Coming Back Up

This is what makes recursion interesting.

First, the function calls go down the tree.

1
↓
2
↓
4
↓
null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

But the answers are built while returning back up.

null → 0
4    → 1
2    → 2
1    → 3
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

So recursion isn't only

Keep calling the same function.

There are really two directions.

Go down
  ↓
Reach the base case
  ↓
Return values back up
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The base case here is

if (root === null) {
  return 0;
}
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Without it, the recursion would have no place to stop.

Complexity

Every node is visited once.

Time: O(n)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

The recursive call stack depends on the height of the tree.

Space: O(h)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

where h is the height of the tree.

For a balanced tree, that is roughly

O(log n)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

In the worst case, if the tree looks like a linked list

1
 \
  2
   \
    3
     \
      4
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

the depth can become

O(n)
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

👀 Let's View View It

Recursion is probably my favorite example for visualization.

Because the final implementation is tiny

const leftDepth = maxDepth(root.left);
const rightDepth = maxDepth(root.right);

return Math.max(leftDepth, rightDepth) + 1;
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

But a lot is hidden inside those function calls.

When reading the code, it can feel like

maxDepth()
inside maxDepth()
inside maxDepth()
inside maxDepth()
...
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Where are we now? 😿

When we step through the execution, we can follow both parts

Going down

1
↓
2
↓
4
↓
null
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

and then

Coming back

null → 0
↓
4 → 1
↓
2 → 2
↓
1 → 3
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

That makes the recursive idea much easier to see.

Ask the smaller subproblems for their answers, then use those answers to build the current answer.

🌳😸


🧠 What Did We Actually Learn?

These three problems look completely different.

But each one introduces a very useful way of thinking.

Valid Parentheses

Use a stack when the most recent item needs to be handled first.

What was the last thing I opened?
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Reverse Linked List

When changing references, save what you still need before breaking the old connection.

Where do I need to go next before I change this pointer?
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

Maximum Depth of Binary Tree

Break a problem into smaller versions of the same problem.

Can I get the answers from my children and build my answer from them?
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

This is one reason I like learning these problems together.

The implementations are not very large.

But each one introduces a completely different mental model

Stack
Pointer
Recursion
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And those mental models are much harder to learn than the syntax itself.

Sometimes the code tells us what happens.

But I also want to see how it happens.

I want to view it. 👀👀


🎯 Conclusion

In this article, we looked at:

  • Valid Parentheses with a stack
  • Reverse Linked List with pointer manipulation
  • Maximum Depth of Binary Tree with recursion

And more importantly, we followed the state while each algorithm was running.

We watched change the stack.

stack.push() / stack.pop()
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

We watched move through a linked list.

prev
current
next
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

And we watched recursive calls travel down a tree and return their answers back up.

This is exactly the kind of thing I built DSA View View for.

You can write or load a TypeScript implementation, run it with your own inputs, and move backward and forward through the runtime.

If you are learning DSA too, try viewing one of these problems step by step.

Especially if a solution feels like

I understand every line individually... but somehow I still don't understand the whole thing. 😿

Seeing the runtime may connect those pieces together.

And if there is a DSA problem you want me to cover next, please let me know in the comments!

I still have many algorithms to learn myself. 😸

Let's train our DSA muscles together! 💪

If you like DSA View View, please give it a star ⭐

GitHub logo nyaomaru / dsa-view-view

DSA View View allows you to understand DSA to see the data flow. 👀👀 Of course, it's free.

DSA View View

DSA View View

DSA View View turns TypeScript algorithm functions into step-by-step visual stories. 👀👀

DSA View View TV

Write code, run it with structured inputs, and see the arrays, matrices trees, lists, stacks, pointers, and return values move as the function executes.

It is built for those moments when reading the code is not enough and you want to view why the answer changes.

DSA View View demo

Why Try It?

  • 🧠 Step through real TypeScript
    Paste or edit a function, validate it, then run the exact code in the browser.

  • 🧩 Views that match the data
    Arrays become bars, matrices become grids, trees become node graphs, linked lists become chains, and two-pointer area problems get their own visual view.

  • 🌳 DSA-friendly inputs out of the box
    TreeNode, ListNode, MinHeap, MaxHeap, PriorityQueue, nested arrays matrices, strings, numbers, and class-style inputs are supported without ceremony.

  • 🔎 39 built-in examples
    Search by name, browse…

See you in the next article!

Top comments (5)

🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025
 
nazar-boyko profile image
Nazar Boyko •

Small thing in the parentheses code: the 'stack.pop()!' hides the empty stack case. For input like ')(' the pop returns undefined and the comparison still fails, so the result is right but the non-null assertion makes it look like that path can't happen. An explicit 'if (stack.length === 0) return false' before the pop might be nicer for the visualizer too, since then you can see that exact step go red.

🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025
 
nyaomaru profile image
nyaomaru •

Good catch! Thank you for pointing this out! 😸

The non-null assertion only changes TypeScript’s static view of the value. It does not provide any runtime guarantee that stack.pop() cannot return undefined.

For this example, making the empty-stack case explicit is clearer:

if (stack.length === 0) return false;

const target = stack.pop();

if (target !== pairs[char]) {
  return false;
}
🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025 🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025

This also improves the visualization: the empty-stack guard becomes a distinct return path, so learners can see exactly why execution stops before pop() is called.

I’ve tracked this in GitHub issue #68, and I’ll update the sample as a follow-up.

Thanks again for the careful feedback! 😸

🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025
 
nazar-boyko profile image
Nazar Boyko •

Glad it helped! Thanks for the article, the visualizations are great! 🙃

🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025
 
kenielzep97 profile image
Self-Correcting Systems •

the framing is the part i think is doing the most work here. three problems chosen so each one carries a different idea, stack then pointer then recursion, instead of three problems that happen to be on the same list. that ordering is the lesson and the visualizer is what makes it stick.

one thing i noticed running the parentheses one, and it might be the best argument for your tool that exists in the post.

the pop is written stack.pop()! and that ! tells typescript undefined cannot happen here. for the input ")" it does happen. the stack is empty, pop returns undefined, and the answer still comes out right because undefined !== "(" is true, so it returns false either way.

so the assertion is wrong and the program is correct, and nothing in the code or the types will ever tell you. but a step through view would show target = undefined sitting right there. that is a state the annotation said does not exist. watching your implementation actually run catches the thing that reading it cannot, which is exactly what you built this for.

small idea for that view: at the step where it fails, show target and pairs[char] next to each other rather than only the stack. for ([)] the thing a learner needs is which comparison disagreed, and the stack alone shows the symptom instead of the mismatch.

nice work, looking forward to the next three.

🔥 ข่าวด่วน: nyaomaru/learn valid parentheses reverse linked list and tree max depth with step by step visualization in o - Kapook Update 2025
 
nyaomaru profile image
nyaomaru •

Thank you so much for such a thoughtful comment! 😸

Type-level guarantees only go so far. What actually happens at runtime can still be different depending on the real input and state.

That is exactly one of the reasons I built DSA View View, and why I designed it so we can step through the runtime one state at a time. So I’m really happy that this part stood out to you!

And I really like your idea about showing target and pairs[char] side by side at the failing step. That would make the actual mismatch much easier to understand than showing only the stack.

I’d love to try incorporating that. Thank you for the valuable feedback!

Looking forward to learning together again! 😸