Day 78/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Maximum Level Sum of a Binary Tree A classic BFS (level-order traversal) problem on trees. Problem idea: Find the level of the tree that has the maximum sum of node values. Key idea: Level-order traversal using a queue. Why? • We need to process nodes level by level • BFS naturally groups nodes by levels • Easy to compute sum for each level How it works: • Use a queue to perform BFS • For each level: → Process all nodes in current queue size → Calculate the sum of that level • Track the maximum sum and corresponding level • Return the smallest level with maximum sum Time Complexity: O(n) Space Complexity: O(n) Big takeaway: Whenever a problem involves level-wise processing in trees, BFS is the go-to approach. 🔥 Day 78 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #BFS #LevelOrder #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
Yogesh ..’s Post
More Relevant Posts
-
🚀 Day 40 of #100DaysOfDSA (Java) Today was focused on practicing Graph concepts. I attended a live session with my mentor where I: 🔹 Solved graph-based problems using BFS & DFS 🔹 Practiced traversal and path-related questions 🔹 Improved my understanding of graph problem-solving strategies Key takeaway: Graphs require strong conceptual clarity, and solving problems with guidance helps in building the right approach. Practicing BFS and DFS is helping me understand how to explore nodes efficiently and solve connectivity problems. Day 40 ✅ Focused on practice, consistency, and improving problem-solving skills. #DSA #Java #Graphs #BFS #DFS #ProblemSolving #100DaysOfCode #DeveloperJourney #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
**Day 117 of #365DaysOfLeetCode Challenge** Today’s problem: **01 Matrix (LeetCode 542)** We need to find the distance of every cell to the **nearest 0** in a binary matrix. This is a perfect example of **Multi-Source BFS** 💡 **Core Idea:** Instead of running BFS from every `1` (too slow ) We start BFS from **all 0s simultaneously** Why? Because every `0` is already at distance `0`, and BFS expands layer by layer to fill shortest distances. 📌 **Approach:** 1️⃣ Put all cells containing `0` into queue 2️⃣ Mark all `1`s as unvisited (`-1`) 3️⃣ Run BFS: * Explore 4 directions * If neighbor unvisited: * distance = current + 1 * push into queue 💡 This guarantees the first time we reach a cell = shortest path to nearest 0. ⚡ **Time Complexity:** O(m × n) ⚡ **Space Complexity:** O(m × n) **What I learned today:** When shortest distance is needed from **multiple starting points**, think: 👉 Multi-Source BFS This pattern appears in many graph + matrix problems. 💭 **Key Takeaway:** Sometimes reversing the perspective makes the problem easier. Instead of finding nearest zero for every one... 👉 Let all zeros find every one. #LeetCode #DSA #BFS #GraphTheory #Matrix #Java #CodingChallenge #ProblemSolving #TechJourney #Consistency
To view or add a comment, sign in
-
-
Today I solved LeetCode 111 – Minimum Depth of Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, return 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. 💡 Key Concepts Used: Binary Trees Breadth First Search (BFS) Queue data structure Tree traversal 🧠 Approach: Use BFS (level order traversal) with a queue. Start from the root and process nodes level by level. The first time you encounter a leaf node, return the current depth. Since BFS explores level by level, it guarantees the shortest path. Alternative: Use recursion, but carefully handle cases where one subtree is null. ⏱ Time Complexity: O(N) — In the worst case, all nodes are visited. 📚 What I Learned: Why BFS is preferred for shortest path problems in trees. Difference between minimum depth vs maximum depth. Handling edge cases in tree recursion. Strengthening queue-based traversal concepts. Understanding when to use BFS vs DFS is key Consistency builds strong problem-solving skills #LeetCode #DSA #BinaryTree #MinimumDepth #BFS #Queue #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
🚀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
To view or add a comment, sign in
-
-
Day 86 – Binary Tree Level Order Traversal Worked on traversing a binary tree level by level using a queue-based approach (Breadth-First Search). Key Learnings: Learned to use a queue (FIFO) for level-wise traversal of trees Understood how to process nodes layer by layer using queue size Strengthened understanding of tree traversal patterns and data structure usage #DSA #Java #BinaryTree #BFS #Queue #ProblemSolving #CodingPractice
To view or add a comment, sign in
-
-
🚀 Solved Binary Tree Level Order Traversal on LeetCode Today I practiced another important Binary Tree problem using Queue + BFS Traversal in Java. 🌳 🔹 Level Order Traversal Order: Node by Node, Level by Level 🔹 Approach Used: • Used Queue data structure • Inserted root node first • Processed one level at a time • Stored each level in separate list • Added left and right child nodes step by step 📊 Result: ✔️ All test cases passed (35/35) ✔️ Runtime: 1 ms ✔️ Beats 95.37% submissions 💡 Key Learning: DFS helps in depth traversal, but Queue + BFS is perfect when we need level wise output. Understanding when to use Stack, Recursion, or Queue is what makes problem solving stronger. 🔥 Currently improving Trees, Graphs, Binary Search, HashMap patterns, and DP step by step. #DSA #Java #LeetCode #BinaryTree #BFS #Queue #ProblemSolving #CodingJourney #Placements
To view or add a comment, sign in
-
-
🚀 Multi-Source BFS with Conflict Resolution, but A Subtle Twist Solved an interesting problem during LeetCode Weekly Contest 498 (Q3) that looks like a standard BFS at first… but has a catch 👇 You’re given multiple source cells, each with a color. At every time step, colors spread to adjacent cells. But here’s the twist: 👉 If multiple colors reach the same cell at the same time, the cell takes the maximum color. 🧠 Key Insight A normal BFS doesn’t work directly because it processes nodes one by one. That can lead to assigning a smaller color before a larger one arrives in the same time step. The fix? ✅ Process BFS level by level (time steps) ✅ Collect updates for the entire level first ✅ Resolve conflicts using max color ✅ Apply updates after the level finishes 💡 Approach Use multi-source BFS (push all sources initially) For each level: Use a HashMap to track best color per cell Only update the grid after processing the full level ⚡ Complexity Time: O(n × m) Space: O(n × m) 🔥 Takeaway Whenever a problem involves: Simultaneous updates Tie-breaking rules at the same time step 👉 Think in terms of level-order BFS + delayed updates It’s always the “small twist” problems that force you to rethink standard patterns. How would you optimize this further (maybe avoid string keys in HashMap)? 👀 #LeetCode #WeeklyContest #Algorithms #BFS #Java #CodingInterview #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 102/160 – GFG Challenge Today’s problem: Cycle Detection in an Undirected Graph 🔁 🔍 Key Learning: Learned how to detect cycles using BFS traversal Used a queue with (node, parent) to track traversal path Understood why checking neighbor != parent is crucial to avoid false cycle detection 🧠 Concept Breakdown: Convert edge list → adjacency list Traverse all components (important when graph is disconnected) If a visited node is reached again and it's not the parent, a cycle exists 💡 Takeaway: Graph problems become much easier when you: Identify the input format (edge list vs adjacency list) Choose the right traversal (BFS/DFS) Track parent properly in undirected graphs ⏱️ Time Complexity: O(V + E) Consistency is key 🔥 102 days down, 58 more to go! #GFG #100DaysOfCode #DSA #Java #GraphTheory #CodingJourney
To view or add a comment, sign in
-
-
𝑺𝒂𝒎𝒆 𝑻𝒓𝒆𝒆 — 𝑤ℎ𝑒𝑟𝑒 𝐵𝐹𝑆 𝑠𝑡𝑜𝑝𝑝𝑒𝑑 𝑏𝑒𝑖𝑛𝑔 𝑚𝑦 𝑑𝑒𝑓𝑎𝑢𝑙𝑡 Before this problem, I was a bit too fond of BFS. I was literally trying to use it everywhere — at least once in every problem. It felt very intuitive after I explored it in 𝗜𝗻𝘃𝗲𝗿𝘁 𝗕𝗶𝗻𝗮𝗿𝘆 𝗧𝗿𝗲𝗲. So when I saw this problem, my first instinct was the same — use BFS and do level order traversal. The idea was simple: use two queues, one for each tree, and traverse them in parallel. But very quickly, things got messy. At each step, I had to check: • if both nodes are null • if one is null and the other isn’t • if both are non-null, whether their values match And if any of these failed → return false. It was working, but the conditions just kept piling up inside the traversal. The code became harder to read and even harder to reason about. That didn’t feel right. So I tried solving it using DFS with recursion. And the difference was… clear. The code was much shorter, cleaner, and handled all cases naturally. That’s when it clicked: Trees just fit naturally with recursion. Each node can be treated as its own subtree, and you just pass it down. Also… no more BFS spamming now 😅 𝐂𝐨𝐫𝐞 𝐈𝐧𝐬𝐢𝐠𝐡𝐭 : " 𝐽𝑢𝑠𝑡 𝑏𝑒𝑐𝑎𝑢𝑠𝑒 𝑠𝑜𝑚𝑒𝑡ℎ𝑖𝑛𝑔 𝑤𝑜𝑟𝑘𝑠 𝑑𝑜𝑒𝑠𝑛’𝑡 𝑚𝑒𝑎𝑛 𝑖𝑡 𝑓𝑖𝑡𝑠 — 𝑐ℎ𝑜𝑜𝑠𝑒 𝑡ℎ𝑒 𝑎𝑝𝑝𝑟𝑜𝑎𝑐ℎ 𝑡ℎ𝑎𝑡 𝑚𝑎𝑡𝑐ℎ𝑒𝑠 𝑡ℎ𝑒 𝑠𝑡𝑟𝑢𝑐𝑡𝑢𝑟𝑒. " Thanks for reading !! #DSA #ProblemSolving #LearnInPublic NeetCode #NeetCode150 #Java
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
-
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