📅 Day 60 of #100DaysOfLeetCode 🌳 Problem: 1161. Maximum Level Sum of a Binary Tree 🟡 Difficulty: Medium 🧠 Problem Summary Given a binary tree, each level has a sum of its node values. The task is to find the smallest level number whose node sum is maximum. 💡 Approach (BFS / Level Order Traversal) Use a queue to traverse the tree level by level For each level: Calculate the sum of all nodes at that level Compare it with the maximum sum found so far Track the level that gives the maximum sum If multiple levels have the same sum, return the smallest level ⏱️ Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
Binary Tree Maximum Level Sum
More Relevant Posts
-
📅 Day 73 of #100DaysOfLeetCode 🧩 Problem: 3314. Construct the Minimum Bitwise Array I 📊 Difficulty: Easy 🧠 Key Insight For each prime number nums[i], we need to find the smallest non-negative integer x such that: x | (x + 1) == nums[i]. If no such value exists, the answer should be -1. ⚙️ Approach Initialize the answer array with -1 For each number nums[i]: Try all values of x from 0 to nums[i] - 1 Check if x | (x + 1) equals nums[i] As soon as a valid x is found, store it and stop searching If no valid value is found, keep -1 for that index ⏱ Complexity Time: O(n × max(nums[i])) Space: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🚀 DSA Series – Day 29 📌 Problem: Find the Duplicate Number (LeetCode) Today I worked on a classic array problem where the goal is to find the single duplicate number in an array containing n+1 integers from 1 to n. 🧠 What I learned Instead of sorting or nested loops, we can use a frequency array to track how many times each number appears. If a number appears more than once, that is our duplicate. 💡 My Approach -Create an extra array to store counts -Traverse the input array and increase the count -Scan the count array to find the value with frequency > 1 ⏱ Complexity Time: O(n) Space: O(n) #DSA #LeetCode #Java #ProblemSolving
To view or add a comment, sign in
-
-
📅 Day 74 of #100DaysOfLeetCode 🧩 Problem: 3315. Construct the Minimum Bitwise Array II 📊 Difficulty: Medium 🧠 Key Insight For each prime number nums[i], we need to find the minimum integer x such that: x | (x + 1) == nums[i] x | (x + 1) is always odd Therefore, if nums[i] is even, no valid x exists → answer is -1 For odd nums[i], the minimum x can be formed by flipping the lowest 0-bit in nums[i] and setting all lower bits to 1 ⚙️ Approach Initialize the answer array For each number nums[i]: If nums[i] is even → assign -1 Otherwise: Find the lowest bit position where nums[i] has 0 Clear that bit Set all lower bits to 1 Store the resulting value as the minimum valid x ⏱ Complexity Time: O(n) Space: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🔹 Day 98 – LeetCode Practice 📌 Problem: Path Sum II (LeetCode #113) 📊 Difficulty: Medium 🧠 Problem Overview: Given the root of a binary tree and a target sum, the task is to find all root-to-leaf paths where the sum of node values equals the target. Each valid path should be returned as a list of values. ✅ Approach Used: Traversed the tree using depth-first search. Maintained the current path and cumulative sum while moving down the tree. When a leaf node was reached, checked if the sum matched the target. Used backtracking to explore all possible paths correctly. 📈 Submission Results: Status: Accepted ✅ Runtime: 2 ms Memory Usage: 45.54 MB 💡 Reflection: This problem is a great example of how recursion and backtracking work together in tree problems. Managing state carefully is key to collecting all valid paths without duplication. #LeetCode #BinaryTree #DFS #Backtracking #Java #DSA #CodingPractice
To view or add a comment, sign in
-
-
🔹 Day 97 – LeetCode Practice 📌 Problem: Divide Array Into Equal Pairs (LeetCode #2206) 📊 Difficulty: Easy 🧠 Problem Overview: You’re given an integer array containing 2n elements. The goal is to check whether the array can be divided into n pairs such that: Every element is used exactly once Both elements in each pair are equal ✅ Approach Used: Sorted the array to bring identical elements together. Traversed the array while counting occurrences of each number. Verified that every number appears an even number of times, ensuring valid pairs. 📈 Submission Results: Status: Accepted ✅ Runtime: 8 ms Memory Usage: 46.94 MB 💡 Reflection: This problem is a great reminder that sorting can simplify pairing logic significantly. Once the array is ordered, validating pairs becomes straightforward and efficient. #LeetCode #ProblemSolving #Arrays #Java #DSA #CodingPractice #Consistency
To view or add a comment, sign in
-
-
📅 Day 80 of #100DaysOfLeetCode 🧩 Problem: 3819. Rotate Non-Negative Elements Difficulty: Medium 💡 Key Insight Negative elements must act like fixed blockers. So instead of rotating the entire array, we: Extract only non-negative elements Rotate that extracted list Put them back only into non-negative positions This avoids disturbing any negative indices 🚫. 🛠️ Approach Count how many non-negative elements exist (pc). Reduce k using k % pc to avoid unnecessary rotations. Store all non-negative elements in a temporary array. Perform left rotation using the reverse array technique: Reverse first k elements Reverse remaining elements Reverse the whole array Traverse the original array and replace only non-negative positions with rotated values. ⏱️ Complexity Time: O(n) Space: O(pc) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 49 of #100DaysOfCode Solved LeetCode Problem #1339 – Maximum Product of Splitted Binary Tree 🌳 This problem focused on splitting a binary tree into two subtrees such that the product of their sums is maximized. It required a two-pass DFS strategy first to compute the total sum, and second to evaluate every possible split efficiently. Key Learnings: -> Used DFS to calculate subtree sums -> Explored every edge as a potential split point -> Tracked the maximum product using global state -> Applied modulo arithmetic to handle large results Language Used: Java -> Runtime: 7 ms (Beats 47.86%) -> Memory: 60.07 MB Deepening tree recursion skills and optimization thinking, one problem at a time 🚀🌲 #LeetCode #BinaryTree #DFS #Java #ProblemSolving #DSA #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 82 of #100DaysOfLeetCode ✅ Problem: Number of Longest Increasing Subsequence ✅ Difficulty: Medium 💡 Key Insight: Instead of tracking only the LIS length, we also maintain the number of ways each LIS can be formed. For every element, we check all previous smaller elements and update both length and count dynamically. 🛠 Approach: Use two arrays: dp[i] → Length of LIS ending at index i cnt[i] → Number of LIS ending at index i If a longer subsequence is found → update length and copy count. If another subsequence of the same length is found → add the counts. Finally, sum counts for indices having the maximum LIS length. ⏱ Complexity: Time: O(n²) Space: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🚀 DSA Day 3 | Arrays & Prefix Sum (Java) Today’s focus was on Prefix Sum, an important array technique that helps optimize range sum queries efficiently. ✅ What I learned: How to build a prefix sum array Difference between in-place and extra array prefix sums How prefix sum reduces time complexity from O(n) to O(1) for range queries Solved multiple practice problems to strengthen logic 🧠 Key Insight: Pre-computation makes problem solving faster and cleaner. Consistency over motivation. One day at a time 💪 #DSA #Java #PrefixSum #Arrays #CodingJourney #LearningEveryDay #100DaysOfCode
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