Day 21/21 – Maximum Depth of Binary Tree | Recursion Mastery
Автор: SheCodes & Grows
Загружено: 2026-03-04
Просмотров: 11
Описание:
Day 21 of 21 – Challenge Complete
Problem: Maximum Depth of Binary Tree
Task:
Given the root of a binary tree, return the maximum depth — the number of nodes along the longest path from the root to the farthest leaf node.
Key Idea – Recursion (DFS)
Each node asks:
“What is the maximum depth of my left subtree and right subtree?”
Then returns:
1 + max(leftDepth, rightDepth)
Base Case:
If node is null → return 0.
Java Recursive Solution
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return 1 + Math.max(left, right);
}
}
Complexity
Time: O(n)
Space: O(h) → recursion stack
#21DaysOfCode
#Day21
#BinaryTree
#Recursion
#DSA
#Java
#CodingChallenge
#InterviewPrep
#ConsistencyWins
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: