Day 34/100 | #100DaysOfDSA 🌳💻 Today’s problem: Diameter of Binary Tree The diameter of a binary tree is the length of the longest path between any two nodes. This path may or may not pass through the root. Approach: • Use DFS recursion to compute the height of each subtree • For every node, calculate leftHeight + rightHeight • Update the maximum diameter during traversal • Return 1 + max(leftHeight, rightHeight) as the height This works because the longest path through any node is formed by combining the heights of its left and right subtrees. Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Big takeaway: Sometimes the best approach is computing one value (height) while updating another (diameter) during recursion. Learning to combine multiple calculations in a single tree traversal is a powerful pattern. Building stronger tree intuition one problem at a time. 🚀 #100DaysOfCode #LeetCode #DSA #BinaryTree #Trees #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity #SoftwareEngineer #Consistency #Programmers #TechGrowth
Binary Tree Diameter Problem Solution with DFS
More Relevant Posts
-
Day 36/100 | #100DaysOfDSA 🌳💻 Today’s problem: Subtree of Another Tree The task is to determine whether one binary tree is a subtree of another. A subtree must match both the structure and node values of the original tree. Approach: • Traverse the main tree using recursion • At each node, check if the subtree rooted there matches the given subRoot • Use a helper function to compare both trees for structural and value equality • If no match is found, continue checking the left and right subtrees This works by combining two recursive checks: 1️⃣ Search for a potential matching root 2️⃣ Verify if both trees are identical Time Complexity: O(n × m) Space Complexity: O(h) Big takeaway: Complex tree problems often break down into combining simpler tree problems — in this case, tree traversal + same tree comparison. Building stronger tree problem-solving patterns every day. 🚀 #100DaysOfCode #LeetCode #DSA #BinaryTree #Trees #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity #SoftwareEngineer #Consistency #Programmers #TechGrowth
To view or add a comment, sign in
-
-
LeetCode Problem #1920 – Build Array from Permutation Today I solved an interesting array problem that helped me understand indexing and array traversal better. 📌 Problem: Given a 0-indexed array `nums`, build a new array `ans` such that: `ans[i] = nums[nums[i]]` 📌 Approach: * Create a new array `ans` with the same length as `nums` * Traverse the array using a `for` loop * For each index `i`, access the value `nums[i]` * Use that value as an index again to get `nums[nums[i]]` * Store the result in `ans[i]` 📌 Key Concepts Practiced: ✔ Array traversal ✔ Nested indexing ✔ Understanding how values can act as indexes 📌 Time Complexity: O(n) – since we traverse the array only once. 📌 Key Takeaway: Sometimes the value inside an array can also represent an index, and understanding this idea helps solve many array-based problems efficiently. 💡 Learning Note This problem improved my understanding of **index-based operations and array manipulation**, which are fundamental concepts for solving many coding interview questions. #LeetCode #CodingPractice #Java #DSA #Arrays #ProblemSolving
To view or add a comment, sign in
-
-
Day 35/100 | #100DaysOfDSA 🌳💻 Today’s problem: Same Tree The goal is to determine whether two binary trees are identical — meaning they have the same structure and the same node values. Approach: • Use recursion to traverse both trees simultaneously • If both nodes are null → they match • If one node is null and the other isn’t → trees are different • If node values differ → trees are different • Recursively check the left and right subtrees Both trees must match at every node for them to be considered the same. Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Big takeaway: When comparing two trees, traverse them in parallel and validate structure and values at each step. Simple logic, but a great exercise for strengthening recursive tree thinking. One more tree pattern learned. 🚀 #100DaysOfCode #LeetCode #DSA #BinaryTree #Trees #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity #SoftwareEngineer #Consistency #Programmers #TechGrowth
To view or add a comment, sign in
-
-
Day 21 – Binary Tree Maximum Path Sum 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem focused on finding the maximum path sum in a binary tree, which is one of the most important advanced tree recursion problems. Approach 1️⃣ Recursively compute left & right subtree sums 2️⃣ Ignore negative paths: left = Math.max(left, 0) right = Math.max(right, 0) 3️⃣ Update global max: max = Math.max(max, root.val + left + right) . 4️⃣ Return: root.val + max(left, right) . Github Code :- https://lnkd.in/gk3GgN-W #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #Recursion #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day21 #DeveloperJourney
To view or add a comment, sign in
-
-
🚀 Day 8 of #100DaysOfDSA Today’s problem: Longest Substring Without Repeating Characters 🧠 Problem Statement: Given a string, find the length of the longest substring without repeating characters. 💡 Approach: Sliding Window Technique Used two pointers (left & right) Maintained a HashSet to track characters Expanded window when unique Shrunk window when duplicate found ⚡ Key Learning: Efficient use of the sliding window helps reduce time complexity to O(n), which is crucial for optimizing string-based problems. 💻 Example: Input: "abcabcbb" Output: 3 ("abc") 🔥 This problem strengthened my understanding of: ✔️ Two-pointer technique ✔️ Hashing (Set/Map) ✔️ Optimizing brute force solutions Consistency is the key — small steps every day lead to big results 💪 #DSA #Java #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode 🍁 Saidhanya Sree
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟑 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding the maximum product subarray. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Maximum Product Subarray 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Tracked two values at each step: • Current maximum product • Current minimum product • Why both? • A negative number can turn a small product into a large one • For each element: • Calculated new max and min using previous values • Updated the result with the maximum product found 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Negative numbers can flip the result completely • Tracking both max and min is crucial • DP can be optimized using variables instead of arrays • Edge cases (like zero) reset the product 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the minimum value is just as important as the maximum — because it might become the next maximum. 53 days consistent 🚀 On to Day 54. #DSA #Arrays #DynamicProgramming #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟭/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟴. 𝗦𝘂𝗯𝘀𝗲𝘁𝘀 | 𝗠𝗲𝗱𝗶𝘂𝗺 | 𝗝𝗮𝘃𝗮 Given an integer array, return all possible subsets (the power set) — no duplicates allowed. 𝗠𝘆 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗮𝗰𝗸𝘁𝗿𝗮𝗰𝗸𝗶𝗻𝗴: ✅ At each index, make a choice: include or exclude ✅ Recurse with the element added → then backtrack (remove it) ✅ Recurse again without the element ✅ Base case: when index == nums.length, save the current subset This explores every branch of the decision tree, giving us all 2ⁿ subsets cleanly. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n × 2ⁿ) — 2ⁿ subsets, each copied in O(n) 📦 Space: O(n) recursion depth Backtracking is one of those patterns that feels magical once it clicks — the "add → recurse → remove" rhythm is the heart of so many classic problems. 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gZmezt_g 19 more days to go. Almost there! 💪 #LeetCode #Day81of100 #100DaysOfCode #Java #DSA #Backtracking #Recursion #CodingChallenge #Programming
To view or add a comment, sign in
-
🔥 Day 364 – Daily DSA Challenge! 🔥 Problem: 🎯 Find K Closest Elements Given a sorted array arr, an integer k, and a target x, return the k closest elements to x in ascending order. 💡 Key Insight — Binary Search on Window Instead of checking each element, we binary search the starting index of a window of size k. Search space: Possible windows → [0 ... n - k] 🧠 Decision Logic At index mid, compare: Distance of left boundary → x - arr[mid] Distance of right boundary → arr[mid + k] - x We choose direction based on: ⚡ Algorithm Steps ✅ Initialize: left = 0, right = n - k ✅ Binary search: If left side is farther → shift right Else → move left ✅ Final window starts at left ✅ Collect k elements ⚙️ Complexity ✅ Time Complexity: O(log(n - k) + k) (binary search + result building) ✅ Space Complexity: O(k) 💬 Challenge for you 1️⃣ Why do we search on window positions instead of elements? 2️⃣ Can you solve this using a heap approach? 3️⃣ What if array was unsorted? #DSA #Day364 #LeetCode #BinarySearch #SlidingWindow #Arrays #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🔥 Day 356 – Daily DSA Challenge! 🔥 Problem: ⚡ Max Consecutive Ones III Given a binary array nums and an integer k, return the maximum number of consecutive 1s if you can flip at most k zeros. 💡 Key Insight — Sliding Window We maintain a window where the number of zeros is at most k. Core condition: 🧠 How It Works 🔹 Expand the window using right 🔹 Count zeros in the window 🔹 If zeros exceed k → shrink from left 🔹 At every step, window length is a valid answer ⚡ Algorithm Steps ✅ Initialize: left = 0, zero = 0, max = 0 ✅ Traverse with right: If nums[right] == 0 → increment zero While zero > k: shrink window from left Update max length ⚙️ Complexity ✅ Time Complexity: O(n) (each element processed once) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why does this work only for binary arrays? 2️⃣ How would you modify this for longest substring with at most k distinct characters? 3️⃣ What if we want to flip exactly k zeros instead of at most k? #DSA #Day356 #LeetCode #SlidingWindow #TwoPointers #Arrays #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
“Most people try to overcomplicate this one… but the simplest approach wins.” Day 69 — LeetCode Progress Problem: Height Checker Required: Given an array of student heights, return the number of indices where the heights are not in the expected non-decreasing order. Idea: If we sort the array, we get the expected order. Now just compare the original array with the sorted version — mismatches are the answer. Approach: Create a copy of the original array Sort the copied array Traverse both arrays: Compare elements at each index If they differ → increment count Return the count Time Complexity: O(n log n) Space Complexity: O(n) #LeetCode #DSA #Java #Arrays #Sorting #ProblemSolving #CodingJourney
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