Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 06 · DSA · Clinic: search, trees, graphs and heaps · about 14 min to read

Play it as a journeyTutor comes with a plan

Clinic #09: Maximum Depth of Binary Tree

Pattern: Tree Recursion (DFS) · LC 104

Maximum Depth of Binary Tree
Maximum Depth of Binary Tree · Open board

Ek family register jaisa naksha: sabse upar ek naam, uske neeche do naam, unke neeche phir do-do, jab tak record khatam na ho jaaye. Sawaal seedha hai, sabse lambi confirmed line kitni generations tak jaati hai?

Before you start. Itna kaafi hai: upar ek box hota hai, uske neeche zyada se zyada do box latak sakte hain, aur yehi shape neeche tak dohrati hai. Foundations #08: Trees & Recursion isi shape pe poora saancha sikhata hai. Yahan sirf ek specific sawaal us saanche se solve hoga.

Ek genealogist (family history ka record rakhne wala insaan) apna purana family register kholta hai, kisi reunion ke liye. Sabse upar ek naam hai, parivaar ka sabse purana insaan jiska record bacha hai. Uske do bete likhe hain, dono ke aage apne-apne bete, aur wahi silsila neeche chalta hai. Kuch lines bahut lambi chalti hain: pardada, unke bete, unke bete, saat generations tak record milta hai. Kuch beech mein hi ruk jaati hain, kisi bete ka aage koi record nahi mila, register wahi khatam ho jaata hai.

Genealogist ek cheez jaanna chahta hai: sabse lambi confirmed line kitni generations tak jaati hai, register ke saare beton mein se. Register ka har naam engineer ke liye ek node hai, aur har node ke neeche zyada se zyada do node hote hain, bete A, bete B. Isi shape ko engineers binary tree bulate hain. Board ka udaharan bhi yehi hai:

      3
     / \
    9   20        ->   depth = 3   (3 -> 20 -> 15/7)
        / \
       15  7

Jiske aage koi record nahi bacha uski jagah null likhi jaati hai - 9 ke neeche dono taraf null hai, isliye 9 ki line wahi khatam. Sabse lambi line root (3) se sabse door node tak jitne node hain, wahi is sawaal ka jawab hai: max depth.

Lesson 49 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • Register ko ek pointer se kyun nahi gina ja sakta
  • Fix: har naam apne bacchon se poochta hai
  • Constraints
  • Solution Ladder, dono O(n), “best” context pe
  • Naapa hua: dono, n = 10² se 10⁵, do shape ke tree
  • Edge cases, chala ke, dono pe
  • Trace, recursion, jawab neeche se upar
  • Interview mein pehli baar, raasta
  • The Tell
  • Retrieval

Terms it explains: LC 104, node, null, max depth, recursion, base case, tedha (skewed), stack-overflow se bachta, GALAT, recursion (DFS), cleanest, 10,987, 10,327, 9,375, 9,376, andar, 984 KB, 400 KB, aur, neeche se upar, postorder, leaf, dono, koi nahi, crash, Clarify, Chhota tree banao, Self-similarity dekho, INSIGHT, Base case pehle, Code, Edge, Tree recursion, bacchon, combine, Diameter, gehra/skewed, Balanced Binary Tree.

Review DSA