Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT NachrichtenHow to use Xbox mode on your Windows PC(21.09.2026 um 00:30 Uhr)
Sichere ProgrammierungThe Indie Dev Visibility Playbook: From Zero Users to Your First 100(21.09.2026 um 00:08 Uhr)
Sichere ProgrammierungBase, Chat and Reasoning Models: How Are They Different?(21.09.2026 um 00:09 Uhr)
Sichere ProgrammierungHexfield Deck is for Kanban lovers and Markdown believers(21.09.2026 um 00:20 Uhr)
Sichere ProgrammierungPermissions and Authorisation: A Practical Playbook(21.09.2026 um 00:21 Uhr)
Linux Tipps & Hardeningfilet | Terminal File Manager(20.09.2026 um 21:03 Uhr)
IT NachrichtenHow to use Xbox mode on your Windows PC(21.09.2026 um 00:30 Uhr)
Sichere ProgrammierungThe Indie Dev Visibility Playbook: From Zero Users to Your First 100(21.09.2026 um 00:08 Uhr)
Sichere ProgrammierungBase, Chat and Reasoning Models: How Are They Different?(21.09.2026 um 00:09 Uhr)
Sichere ProgrammierungHexfield Deck is for Kanban lovers and Markdown believers(21.09.2026 um 00:20 Uhr)
Sichere ProgrammierungPermissions and Authorisation: A Practical Playbook(21.09.2026 um 00:21 Uhr)
Linux Tipps & Hardeningfilet | Terminal File Manager(20.09.2026 um 21:03 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Vertical Order Traversal

Reagiere als Erste:r — dein Feedback zählt!

Problem Statement

Given the root of a binary tree, return its vertical order traversal.

Rules:

  • Nodes are grouped by their column (Horizontal Distance).
  • If multiple nodes share the same row and column, sort them by value.

Brute Force Intuition

In an interview, you can explain it like this:

Traverse every node, store its row and column information, then sort all collected nodes based on column, row, and value.

Although correct, sorting all nodes together becomes expensive.

Complexity

  • Time Complexity: O(N log N)
  • Space Complexity: O(N)

Moving Towards the Optimal Approach

Observe that every node has:

Column (Horizontal Distance)

+

Row (Depth)

If we store nodes using:

Column

↓

Row

↓

Values

we can build the answer naturally.

Pattern Recognition

Whenever you see:

  • Vertical Traversal
  • Column-wise Traversal
  • Horizontal Distance

Think:

BFS + Ordered Maps

Key Observation

Assign coordinates:

Root

↓

(Row = 0, Col = 0)

Rules:

Left Child

↓

(Row + 1, Col - 1)

--------------------

Right Child

↓

(Row + 1, Col + 1)

Store nodes as:

TreeMap<
    Column,
    TreeMap<
        Row,
        PriorityQueue<Values>
    >
>

Optimal Approach

Step 1

Perform BFS.

Store:

(Node, Row, Column)

Step 2

Insert into:

column



row



priority queue

Step 3

Traverse:

Columns

↓

Rows

↓

Sorted Values

Optimal Java Solution

class Pair {

    TreeNode node;
    int row;
    int col;

    Pair(TreeNode node,
         int row,
         int col) {

        this.node = node;
        this.row = row;
        this.col = col;
    }
}

class Solution {

    public List<List<Integer>> verticalTraversal(TreeNode root) {

        TreeMap<Integer,
        TreeMap<Integer,
        PriorityQueue<Integer>>> map =
                new TreeMap<>();

        Queue<Pair> q = new LinkedList<>();

        q.offer(new Pair(root, 0, 0));

        while (!q.isEmpty()) {

            Pair curr = q.poll();

            map.putIfAbsent(curr.col,
                    new TreeMap<>());

            map.get(curr.col)
               .putIfAbsent(curr.row,
                    new PriorityQueue<>());

            map.get(curr.col)
               .get(curr.row)
               .offer(curr.node.val);

            if (curr.node.left != null)

                q.offer(new Pair(
                        curr.node.left,
                        curr.row + 1,
                        curr.col - 1));

            if (curr.node.right != null)

                q.offer(new Pair(
                        curr.node.right,
                        curr.row + 1,
                        curr.col + 1));
        }

        List<List<Integer>> ans =
                new ArrayList<>();

        for (TreeMap<Integer,
             PriorityQueue<Integer>> rows
             : map.values()) {

            List<Integer> list =
                    new ArrayList<>();

            for (PriorityQueue<Integer> pq
                    : rows.values()) {

                while (!pq.isEmpty())

                    list.add(pq.poll());
            }

            ans.add(list);
        }

        return ans;
    }
}

Dry Run

        3
       / \
      9  20
         / \
        15  7

Coordinates:

9

↓

(-1,1)

3

↓

(0,0)

15

↓

(0,2)

20

↓

(1,1)

7

↓

(2,2)

Vertical Order:

9

↓

3 15

↓

20

↓

7

Answer:

[[9],[3,15],[20],[7]]

Why BFS + Ordered Maps Work?

Every node is assigned:

Column

↓

Row

TreeMap automatically sorts:

Columns

↓

Rows

PriorityQueue handles nodes that share the same row and column.

Complexity Analysis

Metric Complexity
Time Complexity O(N log N)
Space Complexity O(N)

Interview One-Liner

Perform BFS while tracking row and column indices, then store nodes in nested TreeMaps with a PriorityQueue to maintain the required ordering.

Pattern Learned

BFS

↓

(Row, Column)

↓

Ordered Maps

↓

Vertical Traversal

Similar Problems

  • Vertical Order Traversal
  • Top View
  • Bottom View
  • Vertical Sum
  • Diagonal Traversal

Memory Trick

Think:

Node

↓

(Row, Column)

↓

TreeMap

↓

PriorityQueue

↓

Answer

Mental Model

Tree

↓

Assign Coordinates

↓

Group By Column

↓

Sort By Row

↓

Output

Whenever you hear:

"Vertical Order Traversal"

your brain should immediately think:

BFS + (Row, Column) Coordinates + TreeMap

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Vertical Order Traversal

Thematisch verwandte Begriffe: Vertical, Order, Traversal · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94084 | Suricata before 8.0.7 has an Http2ThreadMultiBuf use-after-free when a t…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick