✅ 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
Solved LeetCode 104: Maximum Depth of Binary Tree with Java Recursion
More Relevant Posts
-
✅ 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
To view or add a comment, sign in
-
-
✅ Day 34 of #100DaysOfCode Challenge 📘 LeetCode Problem 111: Minimum Depth of Binary Tree 🧩 Problem Statement: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Example: Input: root = [3,9,20,null,null,15,7] Output: 2 Explanation: The shortest path is 3 → 9, so the minimum depth = 2. 💡 Approach (Simple Recursive): If the tree is empty → return 0. Recursively find the left and right subtree depths. If one side is missing, the depth = 1 + depth of the other side. Otherwise, take 1 + min(leftDepth, rightDepth). 💻 Java Code (Easy Version): class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); // If one child is missing, take the non-null side if (left == 0 || right == 0) return 1 + left + right; return 1 + Math.min(left, right); } } ⚙ Complexity: ⏱ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Growing one problem at a time 🌱 #Day34 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 34 of #100DaysOfCode Challenge 📘 LeetCode Problem 111: Minimum Depth of Binary Tree 🧩 Problem Statement: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Example: Input: root = [3,9,20,null,null,15,7] Output: 2 Explanation: The shortest path is 3 → 9, so the minimum depth = 2. 💡 Approach (Simple Recursive): If the tree is empty → return 0. Recursively find the left and right subtree depths. If one side is missing, the depth = 1 + depth of the other side. Otherwise, take 1 + min(leftDepth, rightDepth). 💻 Java Code (Easy Version): class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); // If one child is missing, take the non-null side if (left == 0 || right == 0) return 1 + left + right; return 1 + Math.min(left, right); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Growing one problem at a time 🌱 #Day34 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 34 of #100DaysOfCode Challenge 📘 LeetCode Problem 111: Minimum Depth of Binary Tree 🧩 Problem Statement: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Example: Input: root = [3,9,20,null,null,15,7] Output: 2 Explanation: The shortest path is 3 → 9, so the minimum depth = 2. 💡 Approach (Simple Recursive): If the tree is empty → return 0. Recursively find the left and right subtree depths. If one side is missing, the depth = 1 + depth of the other side. Otherwise, take 1 + min(leftDepth, rightDepth). 💻 Java Code (Easy Version): class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); // If one child is missing, take the non-null side if (left == 0 || right == 0) return 1 + left + right; return 1 + Math.min(left, right); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Growing one problem at a time 🌱 #Day34 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 33 of #100DaysOfCode Challenge 📘 LeetCode Problem 110: Balanced Binary Tree 🧩 Problem Statement: Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Explanation: Both left and right subtrees of every node differ in height by at most 1. 💡 Approach (Simple & Efficient): Use a recursive function to calculate the height of each subtree. If any subtree is unbalanced, return -1. Otherwise, return the height of the tree. If the final result is -1 → tree is not balanced. 💻 Java Code: class Solution { public boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return 1 + Math.max(leftHeight, rightHeight); } } ⚙ Complexity: ⏱ Time: O(n) → Each node visited once 💾 Space: O(h) → Recursion stack (h = tree height) 🌱 Another step towards mastering Binary Trees! #Day33 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #BalancedTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ 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
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 33 of #100DaysOfCode Challenge 📘 LeetCode Problem 110: Balanced Binary Tree 🧩 Problem Statement: Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Explanation: Both left and right subtrees of every node differ in height by at most 1. 💡 Approach (Simple & Efficient): Use a recursive function to calculate the height of each subtree. If any subtree is unbalanced, return -1. Otherwise, return the height of the tree. If the final result is -1 → tree is not balanced. 💻 Java Code: class Solution { public boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return 1 + Math.max(leftHeight, rightHeight); } } ⚙️ Complexity: ⏱️ Time: O(n) → Each node visited once 💾 Space: O(h) → Recursion stack (h = tree height) 🌱 Another step towards mastering Binary Trees! #Day33 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #BalancedTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 33 of #100DaysOfCode Challenge 📘 LeetCode Problem 110: Balanced Binary Tree 🧩 Problem Statement: Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Explanation: Both left and right subtrees of every node differ in height by at most 1. 💡 Approach (Simple & Efficient): Use a recursive function to calculate the height of each subtree. If any subtree is unbalanced, return -1. Otherwise, return the height of the tree. If the final result is -1 → tree is not balanced. 💻 Java Code: class Solution { public boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return 1 + Math.max(leftHeight, rightHeight); } } ⚙️ Complexity: ⏱️ Time: O(n) → Each node visited once 💾 Space: O(h) → Recursion stack (h = tree height) 🌱 Another step towards mastering Binary Trees! #Day33 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #BalancedTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development