From 746c71d91e4edc3823e9f9a708f762400420e632 Mon Sep 17 00:00:00 2001 From: geya-1 <23b01a05j3@svecw.edu.in> Date: Wed, 15 Oct 2025 23:21:46 +0530 Subject: [PATCH 1/2] Added Maximum Depth of Binary Tree question and solution --- .../Maximum_Depth_of_Binary_Tree.md | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 PYTHON INTERVIEW/Maximum_Depth_of_Binary_Tree.md diff --git a/PYTHON INTERVIEW/Maximum_Depth_of_Binary_Tree.md b/PYTHON INTERVIEW/Maximum_Depth_of_Binary_Tree.md new file mode 100644 index 0000000..7bac58f --- /dev/null +++ b/PYTHON INTERVIEW/Maximum_Depth_of_Binary_Tree.md @@ -0,0 +1,62 @@ +# 🧩 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. + +--- + + +--- + +## 💡 Explanation +The longest path from the root node (3) to the farthest leaf node is either: +- `3 → 9` → depth = 2 +- or `3 → 20 → 15` / `3 → 20 → 7` → depth = 3 + +Hence, the **maximum depth = 3**. + +--- + +## 🐍 Python Solution + +```python +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def maxDepth(self, root: TreeNode) -> int: + # Base case: if the tree is empty + if not root: + return 0 + + # Recursively find depth of left and right subtrees + left_depth = self.maxDepth(root.left) + right_depth = self.maxDepth(root.right) + + # Return max depth between left and right, plus 1 for current node + return 1 + max(left_depth, right_depth) + + + +Approach: + + If the current node is None, return 0. + + Recursively compute the depth of the left and right subtrees. + + Add 1 for the current node level. + + Return the maximum of the two subtree depths. + +⏱️ Time Complexity + +O(n) — Every node is visited once. + +💾 Space Complexity + +O(h) — Due to recursive call stack, where h is the height of the tree. + From a039e40831999c2ab562b673d60fd87784e8b7b2 Mon Sep 17 00:00:00 2001 From: geya-1 <23b01a05j3@svecw.edu.in> Date: Wed, 15 Oct 2025 23:28:28 +0530 Subject: [PATCH 2/2] Replaced Python solution with Java solution for Maximum Depth of Binary Tree --- .../Maximum_Depth_of_Binary_Tree.md | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/PYTHON INTERVIEW/Maximum_Depth_of_Binary_Tree.md b/PYTHON INTERVIEW/Maximum_Depth_of_Binary_Tree.md index 7bac58f..ed6dbd8 100644 --- a/PYTHON INTERVIEW/Maximum_Depth_of_Binary_Tree.md +++ b/PYTHON INTERVIEW/Maximum_Depth_of_Binary_Tree.md @@ -4,41 +4,36 @@ 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. ---- - --- - -## 💡 Explanation -The longest path from the root node (3) to the farthest leaf node is either: -- `3 → 9` → depth = 2 -- or `3 → 20 → 15` / `3 → 20 → 7` → depth = 3 - -Hence, the **maximum depth = 3**. - ---- - -## 🐍 Python Solution - -```python -class TreeNode: - def __init__(self, val=0, left=None, right=None): - self.val = val - self.left = left - self.right = right - -class Solution: - def maxDepth(self, root: TreeNode) -> int: - # Base case: if the tree is empty - if not root: - return 0 - - # Recursively find depth of left and right subtrees - left_depth = self.maxDepth(root.left) - right_depth = self.maxDepth(root.right) - - # Return max depth between left and right, plus 1 for current node - return 1 + max(left_depth, right_depth) +// Definition for a binary tree node +class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int val) { + this.val = val; + this.left = null; + this.right = null; + } +} + +class Solution { + public int maxDepth(TreeNode root) { + // Base case: if the tree is empty + if (root == null) { + return 0; + } + + // Recursively find depth of left and right subtrees + int leftDepth = maxDepth(root.left); + int rightDepth = maxDepth(root.right); + + // Return max depth between left and right, plus 1 for current node + return 1 + Math.max(leftDepth, rightDepth); + } +}