🚀Day 40 LeetCode 1161 – Maximum Level Sum of a Binary Tree Ever wondered how to find the level of a binary tree with the maximum sum of node values? 🤔 Here’s the idea: We traverse the tree level by level (BFS) and compute the sum at each level. The trick is to keep track of the maximum sum and return the smallest level where this occurs. 💡 Key Insight: Level-order traversal (using a queue) naturally processes nodes one level at a time, making it perfect for this problem. 🔧 Approach: ✔ Use a queue for BFS ✔ For each level: • Calculate sum of nodes ✔ Track maximum sum and corresponding level 🔥 Takeaway: Whenever a problem involves levels in a tree, think BFS first — it often leads to the cleanest solution. #LeetCode #DataStructures #Java #CodingInterview #BinaryTree #BFS #ProblemSolving
Maximize Binary Tree Level Sum with BFS
More Relevant Posts
-
🚀 Day 70 of #100DaysOfCode Solved 107. Binary Tree Level Order Traversal II on LeetCode 🔗 🧠 Key Insight: This is just level order traversal (BFS) but: 👉 Return levels from bottom to top instead of top to bottom ⚙️ Approach (DFS with Level Tracking): 1️⃣ Start traversal with level = 0 2️⃣ For each node: 🔹 If level not present → insert new list at front 🔹 Add value at correct position: 👉 res.get(res.size() - 1 - level) 3️⃣ Recurse: 🔹 Left → level + 1 🔹 Right → level + 1 ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #BinaryTree #BFS #DFS #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Today I solved LeetCode 958 – Check Completeness of a Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, determine if it is a complete binary tree. A complete binary tree is defined as: All levels are completely filled except possibly the last level In the last level, nodes are as far left as possible 💡 Key Concepts Used: Binary Trees Breadth First Search (BFS) Queue data structure Tree properties 🧠 Approach: Use BFS (level order traversal) with a queue. Traverse nodes level by level. Once a null node is encountered: All following nodes must also be null. If a non-null node appears after a null, the tree is not complete. Otherwise, the tree satisfies completeness. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Understanding the properties of a complete binary tree. Using BFS to validate structural constraints. Handling null conditions carefully during traversal. Strengthening queue-based tree problem-solving. Building strong fundamentals in tree structures Consistency is key to mastering DSA #LeetCode #DSA #BinaryTree #CompleteBinaryTree #BFS #Queue #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Day 64/100 🚀 | #100DaysOfDSA Solved LeetCode 49 – Group Anagrams today. The problem was to group strings that are anagrams of each other, meaning they contain the same characters but in different orders. Approach: Used a HashMap with a custom frequency-based key to group anagrams. • Iterated through each string in the array. • Created a frequency array of size 26 for each string. • Built a unique key using the frequency array (joined with a delimiter). • Used this key in the HashMap to group all anagrams together. • Finally returned all grouped values from the map. This avoids sorting each string and gives a more optimal approach. Time Complexity: O(n * k) (where n = number of strings, k = length of each string) Space Complexity: O(n * k) Key takeaway: Instead of sorting strings for anagram problems, using a frequency count as a hash key can be more efficient and avoids extra overhead. #100DaysOfDSA #LeetCode #DSA #Java #Strings #HashMap #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 60 of #100DaysOfCode Solved 100. Same Tree on LeetCode 🔗 🧠 Key Insight: Two trees are the same if: 👉 Their structure is identical 👉 Corresponding nodes have equal values ⚙️ Approach (Recursive DFS): 1️⃣ If both nodes are null → ✅ return true 2️⃣ If one is null and other is not → ❌ return false 3️⃣ If values are different → ❌ return false 4️⃣ Recursively check: 🔹 Left subtree → isSame(p.left, q.left) 🔹 Right subtree → isSame(p.right, q.right) 5️⃣ Return: 👉 leftCheck && rightCheck ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursive stack) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DFS #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Day 34/50 🚀 — Score of a String Today’s problem was all about observing patterns in characters and keeping the approach simple. 🔹 Compared ASCII values of adjacent characters 🔹 Used absolute difference to calculate contribution 🔹 Built the result in a single pass (no extra space) Key insight: Sometimes you don’t need complex data structures—just a clean loop and understanding how characters behave as numbers. 💡 Simple logic, implemented cleanly, is often the most efficient solution. Performance: ⚡ Runtime: 1 ms (99%+) 📦 Memory: Efficient #Day34 #50DaysOfCode #Java #DSA #ProblemSolving
To view or add a comment, sign in
-
-
Day 41 of Daily DSA 🚀 Solved LeetCode 20: Valid Parentheses ✅ Problem: Given a string containing only (), {}, [], determine if the input string is valid. Rules: Open brackets must be closed by the same type Open brackets must be closed in the correct order Every closing bracket must have a matching opening bracket Approach: Used a Stack to track opening brackets and validate matching pairs. Steps: Traverse the string Push opening brackets onto the stack For closing brackets → check top of stack If it matches → pop Else → return false At the end, stack should be empty ⏱ Complexity: • Time: O(n) • Space: O(n) 📊 LeetCode Stats: • Runtime: 3 ms (Beats 87.41%) ⚡ • Memory: 43.37 MB A classic stack problem that builds strong fundamentals for expression parsing & validation. #DSA #LeetCode #Java #Stack #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 64 of #100DaysOfCode Solved 222. Count Complete Tree Nodes on LeetCode 🔗 🧠 Key Insight: In a complete binary tree, all levels are fully filled except possibly the last, and nodes are as left as possible. 👉 This property helps us optimize beyond simple traversal ⚙️ Approach (Simple DFS - Your Solution): 1️⃣ If root is null → return 0 2️⃣ Recursively count: 🔹 left = countNodes(root.left) 🔹 right = countNodes(root.right) 3️⃣ Total nodes: 👉 1 + left + right ⏱️ Time Complexity: Current → O(n) Optimized → O(log² n) 📦 Space Complexity: O(h) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DivideAndConquer #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 61/100 — #100DaysDSAChallenge Today I worked on a classic problem: Implement Queue using Stacks. 💡 The key idea We use two stacks: 1. One stack for input (push operations) 2. One stack for output (pop/peek operations) When we need to remove an element: If the output stack is empty, we move all elements from input stack to output stack This reverses the order Now the oldest element is on top and ready to be removed ⚙️ Complexity ⏱️ Amortized Time: O(1) 💾 Space: O(n) 🧠 What I learned today Today’s lesson was about reversing data flow to achieve a different behavior. #Java #DSA #Stack #Queue #LeetCode #ConsistencyCurve
To view or add a comment, sign in
-
-
Day 44🚀 Solved Number of Islands — a classic DFS/BFS problem that really tests grid traversal and thinking in connected components. 💡 Key Takeaways: Learned how to treat the grid like a graph Used DFS to explore and mark visited land Strengthened understanding of recursion + boundary handling ⚡ Result: ✅ All test cases passed ⏱️ Optimized runtime 📈 Improving problem-solving speed day by day Consistency > Motivation. Showing up daily is the real win. #Day44 #LeetCode #DSA #Java #CodingJourney #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
Day 65/100 🚀 | #100DaysOfDSA Solved LeetCode 148 – Sort List today. The problem was to sort a linked list in O(n log n) time and constant space complexity. Approach: Used Merge Sort on Linked List, since it works efficiently with linked structures. • Base case: if the list is empty or has one node, return it. • Used slow and fast pointers to find the middle of the list. • Split the list into two halves. • Recursively sorted both halves. • Merged the two sorted halves using a helper merge function. The merge step compares nodes and builds a sorted list using a dummy node. Time Complexity: O(n log n) Space Complexity: O(log n) (due to recursion stack) Key takeaway: Merge Sort is the best choice for linked lists because it avoids random access and works efficiently by splitting and merging nodes. #100DaysOfDSA #LeetCode #DSA #Java #LinkedList #MergeSort #ProblemSolving #Consistency
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