"Day 31 of #100DaysOfCode: LeetCode 104 - Maximum Depth of Binary Tree"

✅ Day 31 of #100DaysOfCode Challenge 📘 LeetCode Problem 104: Maximum Depth of Binary Tree 🧩 Problem Statement: Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Example: Input: root = [3,9,20,null,null,15,7] Output: 3 Explanation: The longest path is 3 → 20 → 7, so the depth = 3. 💡 Approach: We can solve this using Recursion — If the tree is empty → depth = 0 Recursively find the depth of the left and right subtrees The maximum depth = 1 + max(leftDepth, rightDepth) 💻 Java Code: class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); return 1 + Math.max(leftDepth, rightDepth); } } ⚙️ Complexity Analysis: ⏱️ Time Complexity: O(n) → Each node is visited once 💾 Space Complexity: O(h) → Call stack height (h = height of tree) 🌱 Another small step in the 100 Days of Code journey! #100DaysOfCode #Day31 #LeetCode #Java #DSA #CodingChallenge #BinaryTree #Recursion

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories