Solved: Binary Tree Maximum Path Sum (LeetCode – Hard) I recently solved the Maximum Path Sum problem in a binary tree, a classic interview question that strengthens recursion and tree traversal concepts. ->Used postorder traversal (process left → right → node). ->Ignored negative subtree sums to avoid reducing the total. ->Maintained a global variable to track the maximum path found so far. ->Returned only the best single-branch path to the parent node. Key Takeaways: 1. Clear understanding of recursion flow is essential. 2. Careful handling of negative values improves correctness. Time Complexity: O(N) Space Complexity: O(H) #LeetCode #Java #BinaryTree #DataStructures #CodingInterview
Binary Tree Maximum Path Sum LeetCode Solution
More Relevant Posts
-
`✅ LeetCode Top Interview 150 – Day 73 Today I solved a classic Binary Tree problem: Validate Binary Search Tree (BST). key idea : Start with range. • Left subtree nodes must be strictly smaller than the root • Right subtree nodes must be strictly greater than the root • Both subtrees must also follow the BST property recursively This problem strengthened my understanding of tree traversal and constraints propagation in recursive algorithms. #LeetCode #DSA #BinaryTree #BinarySearchTree #Java #CodingPractice #ProblemSolving
To view or add a comment, sign in
-
-
Day 23 – Find First and Last Position in Sorted Array Solved this Binary Search problem by finding the first and last occurrence of a target in a sorted array with O(log n) complexity. Key Learnings: Applying binary search twice for boundary detection Adjusting search space using left and right updates Writing overflow-safe mid calculation A great exercise in mastering binary search variations. #DSA #BinarySearch #Java #LeetCode #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 88- #100DaysOfCode Today I solved Peak Index in a Mountain Array using Binary Search. 🔹 Problem Idea A mountain array increases strictly and then decreases. The goal is to find the index of the peak element. 🔹 Approach Used: Binary Search Instead of checking every element (O(n)), we can use the mountain property: • If arr[mid] > arr[mid+1] → we are in the descending part, so the peak lies on the left side. • Otherwise → we are in the ascending part, so move to the right side. This helps us find the peak in O(log n) time. 📊 Time Complexity: O(log n) 📦 Space Complexity: O(1) #DSA #Java #BinarySearch #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 22 of #100DaysOfCode Solved 34. Find First and Last Position of Element in Sorted Array on LeetCode 🔍 🧠 Key insight: To find both boundaries of a target in a sorted array, a single binary search isn’t enough. Running two binary searches—one for the leftmost and one for the rightmost occurrence—gives an optimal solution. ⚙️ Approach: 🔹Perform a binary search to find the first occurrence 🔹Perform another binary search to find the last occurrence 🔹Adjust search boundaries after finding the target to expand left/right ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟭𝟳/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved Isomorphic Strings using a HashMap for character mapping. ➤ Approach (O(n), O(n) space): • If lengths are different → return false • Traverse both strings together • Map each character from s to t • If a mapping already exists, ensure it matches • Also ensure two characters from s don’t map to the same character in t ➤ Key Insight: Isomorphic means maintaining a one-to-one mapping. The mapping must be consistent and unique. ➤ Two checks matter: • Existing mapping should match • No two characters map to the same target #LeetCode #Java #DSA #HashMap #StringProblems #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
🚀 Day 40 of #100DaysOfCode Solved 378. Kth Smallest Element in a Sorted Matrix on LeetCode 🔍📊 🧠 Key Insight: Each row and column in the matrix is sorted, so instead of flattening and sorting the matrix, we can apply Binary Search on the value range. ⚙️ Approach: 1️⃣ Define the search range: 🔹left = smallest element (matrix[0][0]) 🔹right = largest element (matrix[n-1][n-1]) 2️⃣ Perform binary search on values: 🔹Pick mid 🔹Count how many elements in the matrix are ≤ mid 3️⃣ If count ≥ k → move left side (potential answer found) 4️⃣ Else → move right side This efficiently narrows down the kth smallest element. ⏱️ Time Complexity: O(n log(max-min)) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Matrix #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 40/100 | #100DaysOfDSA 💻🔥 Today’s problem: Subarray Sum Equals K Goal: Find the total number of subarrays whose sum equals k. Approach: Prefix Sum + HashMap Idea: • Keep track of the running sum while traversing the array • If (currentSum - k) was seen before, a subarray with sum k exists • Use a HashMap to store prefix sums and their frequencies Key insight: If prefixSum[j] - prefixSum[i] = k, then the subarray between them sums to k. Time Complexity: O(n) Space Complexity: O(n) Big takeaway: Prefix sums combined with hashing can turn many subarray problems from O(n²) to O(n). Day 40 — still stacking patterns. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #PrefixSum #HashMap #Java #CodingJourney #InterviewPrep #ProblemSolving
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 57 / 100 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 🚀 Today’s problem: 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 153 – 𝐅𝐢𝐧𝐝 𝐌𝐢𝐧𝐢𝐦𝐮𝐦 𝐢𝐧 𝐑𝐨𝐭𝐚𝐭𝐞𝐝 𝐒𝐨𝐫𝐭𝐞𝐝 𝐀𝐫𝐫𝐚𝐲 A classic binary search problem that strengthens 𝐥𝐨𝐠𝐢𝐜, 𝐩𝐫𝐞𝐜𝐢𝐬𝐢𝐨𝐧, 𝐚𝐧𝐝 𝐨𝐩𝐭𝐢𝐦𝐢𝐳𝐚𝐭𝐢𝐨𝐧 𝐭𝐡𝐢𝐧𝐤𝐢𝐧𝐠. 🔹 What I practiced today: Applying 𝐛𝐢𝐧𝐚𝐫𝐲 𝐬𝐞𝐚𝐫𝐜𝐡 on rotated arrays Understanding 𝐬𝐨𝐫𝐭𝐞𝐝 + 𝐫𝐨𝐭𝐚𝐭𝐞𝐝 patterns Writing 𝐎(𝐥𝐨𝐠 𝐧) 𝐨𝐩𝐭𝐢𝐦𝐢𝐳𝐞𝐝 logic #100DaysOfCode #Day57 #LeetCode #Java #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #DeveloperLife #Consistency
To view or add a comment, sign in
-
-
🚀 Day 514 of #750DaysOfCode 🚀 Today’s problem: 1022. Sum of Root To Leaf Binary Numbers 🌳 This was a really nice combination of: Binary Trees DFS Traversal Bit Manipulation 🧠 Problem Insight Each root-to-leaf path in the binary tree represents a binary number. Example path: 1 → 0 → 1 Represents binary 101 = 5 Instead of storing binary as a string and converting later ❌ We can build the number directly while traversing: current = current * 2 + node.val This works because: Multiplying by 2 shifts bits left Adding node value appends the current bit Clean. Efficient. Interview-friendly. ✅ ⏱ Complexity Time: O(N) Space: O(H) (recursion stack) 💡 Key Takeaway When dealing with binary paths: 👉 Think in terms of bit shifting, not string building. Small optimization. Big impact in interviews. Consistency > Motivation. 514 days done. Still building. 💪 #LeetCode #Java #BinaryTree #DFS #BitManipulation #DataStructures #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode Solved 154. Find Minimum in Rotated Sorted Array II on LeetCode 🔍 🧠 Key Insight: The array is sorted but rotated, and this version introduces duplicates, which makes the binary search logic slightly trickier. ⚙️ Approach: 🔹Use binary search with left and right pointers 🔹Compare nums[mid] with nums[right]: 🔹If nums[mid] > nums[right] → minimum lies in the right half 🔹If nums[mid] < nums[right] → minimum lies in the left half (including mid) 🔹If equal → safely shrink the search space by decrementing right This handles the ambiguity caused by duplicates. ⏱️ Time Complexity: Average: O(log n) Worst case: O(n) (when many duplicates exist) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
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