I just solved LeetCode 662: Maximum Width of Binary Tree! Finding the width is different from a normal traversal because you have to account for the empty spaces between nodes. I used a simple indexing trick ($2i+1$ and $2i+2$) and an ArrayDeque to track positions. By using peekFirst() and peekLast(), I could instantly calculate the width of each level ($right - left + 1$). It’s a great example of how choosing the right data structure makes the logic much cleaner! #LeetCode #Java #BinaryTree #Coding
Solving LeetCode 662: Maximum Binary Tree Width
More Relevant Posts
-
Day 97/200 – LeetCode Challenge Solved “Largest Rectangle in Histogram” (Hard) today. This problem is a great example of how powerful the Monotonic Stack technique can be. Instead of brute force, we efficiently determine how far each bar can extend to compute the maximum rectangle area. Using a monotonic increasing stack to track indices. Identifying left and right boundaries for each bar. Every day is making data structures feel more intuitive! #Day96 #LeetCode #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
💡 Day 61 of LeetCode Problem Solved! 🔧 🌟 482. Find Maximum Consecutive Ones 🌟 🔗 Solution Code: https://lnkd.in/gf5DPq4E 🧠 Approach: • Traverse the binary array once using one pointer. • Keep counting consecutive 1s and update the maximum value at each step. • When a 0 appears, reset the count to 0. ⚡ Key Learning: • This is a simple sliding window idea. No need for nested loops or extra memory. Tracking current count and maximum value is a useful pattern for many array problems. ⏱️ Complexity: • Time: O(N) • Space: O(1) #LeetCode #Java #DSA #ProblemSolving #Consistency #CodingJourney #Algorithms #Arrays
To view or add a comment, sign in
-
-
Today I solved LeetCode 110 – Balanced Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, determine if it is height-balanced. A binary tree is balanced if: The height difference between the left and right subtree of every node is not more than 1. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Height calculation 🧠 Approach: Use DFS to calculate the height of each subtree. For every node: Get height of left subtree Get height of right subtree Check if the absolute difference is greater than 1: If yes → tree is not balanced Optimize by returning -1 early when imbalance is found to avoid unnecessary calculations. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Combining height calculation with validation in one traversal. Optimizing recursive solutions with early stopping. Understanding balanced vs unbalanced trees. Strengthening DFS and recursion skills. Improving step by step with tree problems Consistency is building confidence every day #LeetCode #DSA #BinaryTree #BalancedTree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
60-Day LeetCode Challenge – Day 43 Solved Check If Two String Arrays are Equivalent on LeetCode. 📌 Approach: Concatenated both string arrays into two strings and compared them using .equals(). 🧠 Learning: Reinforced how string building and comparison works, especially when data is split across arrays. ⚡ Complexity: • Time Complexity: O(n + m) • Space Complexity: O(n + m) Simple logic, but a clean reminder that breaking a problem down makes it easier to solve. #LeetCode #DSA #Java #Strings #Consistency #ProblemSolving #LeetCode60
To view or add a comment, sign in
-
-
🚀 Day 30 of #100DaysOfCode Solved Trapping Rain Water 🌧️ One of those classic problems that really tests your ability to think beyond brute force. Instead of checking every pair, I used the prefix max (leftMax) and suffix max (rightMax) approach to efficiently calculate trapped water. 🔍 Key Takeaways: Precomputing left and right maximum heights simplifies the problem Water trapped at each index = min(leftMax, rightMax) − height[i] Turning intuition into optimized code is the real win here 💻 Complexity: Time: O(n) Space: O(n) Another step forward in mastering arrays and patterns 💪 #DSAwithEdSlash #LeetCode #Java #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 74/100 – LeetCode Challenge 🔍 Problem Solved: Find the Duplicate Number (287) Today’s problem was a great reminder that sometimes the best solutions come from thinking differently 💡 Instead of using extra space or modifying the array, I used Floyd’s Cycle Detection Algorithm (Tortoise & Hare) — treating the array like a linked list to detect a cycle. 👉 Key Learnings: • Arrays can sometimes be visualized as linked structures • Cycle detection is not just for linked lists! • Optimizing for O(1) space is a common interview expectation ⚡ Approach: Use two pointers (slow & fast) First, find intersection point Then, find the cycle start → duplicate number ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) ✅ Successfully passed all test cases! Consistency > Perfection. Let’s keep going 💪 #Day74 #LeetCode #100DaysOfCode #Java #CodingInterview #ProblemSolving #DataStructures #Algorithms
To view or add a comment, sign in
-
-
Today I solved LeetCode 104 – Maximum Depth of Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Tree traversal 🧠 Approach: Use DFS (recursion) to explore the tree. For each node: Recursively calculate the depth of the left subtree. Recursively calculate the depth of the right subtree. The depth of the current node = 1 + max(left depth, right depth) Base case: if node is null, return 0. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Understanding tree depth calculation. Strengthening recursion and DFS concepts. Difference between maximum depth vs minimum depth. Building intuition for tree-based problems. Strong fundamentals in trees make advanced problems easier Consistency is the key to improvement #LeetCode #DSA #BinaryTree #MaximumDepth #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 63 of #100DaysOfLeetCode ✅ Problem Solved: Unique Binary Search Trees (LeetCode 96) Today’s problem was a great example of how Dynamic Programming and mathematical patterns (Catalan Numbers) come together. 🔍 Key Insight: For every node chosen as root, the number of unique BSTs is: 👉 Left Subtrees × Right Subtrees This leads to the recurrence: dp[n] = Σ (dp[left] × dp[right]) 💡 What I learned: Breaking problems into smaller subproblems makes complex structures easier Recognizing patterns like Catalan Numbers is a game changer DP is not just about arrays, it's about thinking smart ⚡ Result: ✔️ Runtime: 0 ms (Beats 100%) ✔️ Clean and optimized solution Consistency is slowly turning into confidence 💪 #LeetCode #DataStructures #DynamicProgramming #CodingJourney #ProblemSolving #Java #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 62 — LeetCode Practice 🚀 ✅ Problem Solved: Sum of Squares of Special Elements 💡 What I learned today: • Understood the concept of 1-based indexing in problems • Learned how to check if an index divides the length of the array • Practiced working with conditions inside loops • Improved clarity on array traversal and mathematical conditions 📊 Approach: • Traverse the array using loop • Convert index to 1-based (i + 1) • Check if n % (i + 1) == 0 • If true → add square of that element to sum ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Small logic, but helps build strong fundamentals in arrays and indexing 💪✨ #Day62 #LeetCode #DSA #Java #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀Day 39 #LeetCode 199 – Binary Tree Right Side View Ever wondered what a binary tree looks like from the right side? 👀 Let’s break it down! 🧠 Problem Insight When you observe a binary tree from the right, at every level you only see one node — the rightmost node. 👉 So the goal is simple: Capture the last node at each level ⚡ Approach (Level Order Traversal - BFS) ✔ Traverse the tree level by level ✔ At each level, pick the last node ✔ Add it to the result 📊 Complexity ⏱ Time: O(n) 📦 Space: O(n) 🔥 Key Takeaway 👉 Rightmost node at each level = Visible node 💡 Example Input: [1,2,3,null,5,null,4] Output: [1,3,4] 💬 Have you tried solving this using DFS (Right-first traversal)? Drop your approach below! #LeetCode #DataStructures #BinaryTree #CodingInterview #Java #Programming
To view or add a comment, sign in
-
Explore related topics
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