Day 97 of #100DaysOfCode Today I solved "Maximum Width of Binary Tree" on LeetCode using Level Order Traversal (BFS) + Indexing. Key Idea: Treat the binary tree like a complete binary tree by assigning indices to nodes: • Left child → 2 * index • Right child → 2 * index + 1 This helps us calculate the width of each level easily. Approach: • Use a queue storing {node, index} • For each level: Take first and last index Width = right - left + 1 • Normalize indices to avoid overflow Concepts Used: • Binary Trees • Breadth First Search (BFS) • Queue • Index-based traversal Time Complexity: O(n) Space Complexity: O(n) This problem was interesting because it combines tree traversal with indexing logic to solve a non-trivial problem From simple traversal → to smart indexing tricks #Day97 #100DaysOfCode #LeetCode #BinaryTree #BFS #Cpp #CodingJourney
Solving Maximum Width of Binary Tree with BFS and Indexing
More Relevant Posts
-
🚀 Day 239 of #500DaysOfCode Solved LeetCode 3488 – Closest Equal Element Queries 🔁 💡 Approach: Store indices of each value using a hashmap Since array is circular, handle wrap-around distance For each query: Use binary search (lower_bound) to find closest index Check neighbors (left & right) Compute min circular distance ⚡ Key Insight: Only adjacent indices in sorted list matter Circular distance = min(|i-j|, n - |i-j|) ⏱️ Complexity: Time: O(n + q log n) Space: O(n) ✨ Takeaway: Combining hashing + binary search + circular logic makes this efficient. Consistency stacking up 🔥 #LeetCode #DSA #BinarySearch #HashMap #Arrays
To view or add a comment, sign in
-
-
I run several Claude Code sessions simultaneously across different projects. Every session starts from scratch. "Which session fixed the partition key bug?" — no way to know without grepping log files. So I built claude-echoes: verbatim semantic memory across Claude Code sessions. It captures every prompt and response, embeds them locally (nomic-embed-text, free, no API keys), and lets you search by meaning: /recall when did we fix the partition key bug We benchmarked it honestly on LongMemEval (ICLR 2025, the standard eval for conversational memory): - 81.0% overall with Sonnet 4.6 - 100% on single-session retrieval (70/70) The whole retrieval layer is ~30 lines of numpy. Full raw outputs and reproduction steps in the repo. MIT licensed. ~800 lines of core code. https://lnkd.in/gGeRWKUx If you use Claude Code daily and keep losing context between sessions, this was built for exactly that pain.
To view or add a comment, sign in
-
-
Day 46 Today I solved: Binary Search (LeetCode 704) 💡 Problem: Given a sorted array, find the index of a target element. If it doesn’t exist, return -1. 💡 My Approach: I used the classic Binary Search technique: 1️⃣ Initialize two pointers: start and end 2️⃣ Find middle index mid 3️⃣ Compare nums[mid] with target 👉 If equal → return mid 👉 If target is greater → search right half 👉 If target is smaller → search left half 4️⃣ Repeat until found or search space is exhausted 💡 Key Insight: Instead of checking every element ❌ Divide the search space in half each time ✅ ⚡ Complexity: Time: O(log n) Space: O(1) #LeetCode #DSA #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
Day 90 of #100DaysOfCode Today I solved "Path Sum III" on LeetCode using a DFS + Recursion approach. Key Idea: We need to count all paths where the sum of node values equals the target. The path doesn’t have to start from the root — it can start from any node! Approach: • For every node, treat it as a starting point • Use DFS to explore all downward paths • Reduce the target at each step (target - node->val) • If a node matches the remaining target → count it • Repeat the process for left and right subtrees Why this works: Every node gets a chance to act as the starting point, and DFS ensures we explore all possible paths efficiently. Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Backtracking (implicit) Time Complexity: O(n²) in worst case Space Complexity: O(h) This problem helped me understand how to explore all possible paths in a tree, not just root-based ones — a big step forward in mastering tree problems From single path problems → to handling multiple dynamic paths… growing every day #Day90 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 91 of #100DaysOfCode Today I solved "Insert into a Binary Search Tree" on LeetCode using a Recursive approach. Key Idea: In a Binary Search Tree (BST): • Left subtree → values less than root • Right subtree → values greater than root So we just follow the correct path until we find the right position to insert the new value. Approach: • If root is null → create and return new node • If value > root → go to right subtree • Else → go to left subtree • Recursively insert until correct position is found Concepts Used: • Binary Search Tree (BST) • Recursion • Tree traversal Time Complexity: O(h) Space Complexity: O(h) This problem reinforces the importance of BST properties for efficient insertion Simple logic, powerful structure #Day91 #100DaysOfCode #LeetCode #BST #Recursion #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 84/150 🚀 LeetCode 124: Binary Tree Maximum Path Sum 🧠 Problem Find the maximum path sum in a binary tree. A path can start and end at any node. 💡 Approach • Use DFS (post-order traversal) • Ignore negative paths → max(0, left/right) • Update global max using left + right + root • Return max single path for recursion ⏱ Time Complexity O(n) 📦 Space Complexity O(h) (recursion stack) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) Hard problems getting comfortable 🌳 #Day84 #LeetCode #BinaryTree #DFS #Recursion #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 21 of my Data Structures & Algorithms Journey 🚀 Solved: Flatten a Multilevel Doubly Linked List (LeetCode 430) 💡 Key Insight: Whenever a node has a child, treat it like a detour — go deep into the child list first, flatten it completely, and then reconnect back to the main list. 🧠 What I learned: • Depth First Search (DFS) approach in linked lists • Handling multiple pointers (next, prev, child) carefully • Importance of saving the next pointer before modifying links • How recursion helps simplify complex structures ⚡ Core Idea: Insert the child list in between the current node and its next node, then reconnect everything smoothly. 🔥 This problem really improved my understanding of pointer manipulation and recursive thinking! #LeetCode #DSA #LinkedList #Recursion #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 6: LeetCode - 540. Single Element in a Sorted Array Today’s problem looked simple at first, but it turned into a really good lesson in pattern observation and binary search. The challenge was to find the single element in a sorted array where every other element appears exactly twice — and do it in O(log n) time. My first instinct was straightforward: compare elements in pairs and return the one that doesn’t match. That works, but it takes O(n) time. The real breakthrough came from observing the pattern: Before the single element, pairs start at even indices After the single element, this pattern shifts Using this idea, I applied binary search: Always check from an even index If a pair is valid → move right If the pattern breaks → move left Takeaway: Binary search is not just about searching values — it’s about identifying and exploiting patterns in the data. Day 6 completed. #DSA #Leetcode
To view or add a comment, sign in
-
-
-- Solved LeetCode 100: Same Tree -- Today I worked on the classic binary tree problem “Same Tree”, and it turned out to be a great exercise in strengthening recursion fundamentals -- Problem Summary: -- Given two binary trees, determine whether they are identical in both structure and node values. -- Approach (Recursive DFS): At each node, I focused on 3 key checks: • If both nodes are NULL → trees match at this point • If one is NULL → structure mismatch • If values differ → not the same If all checks pass, recursively compare left and right subtrees. -- Complexity: Time: O(n) – visiting every node once Space: O(h) – recursion stack (depends on tree height) Consistency > Complexity. #LeetCode #DSA #Recursion #BinaryTree #CodingJourney #SoftwareEngineering #algorithm
To view or add a comment, sign in
-
-
Day 87/150 🚀 LeetCode 199: Binary Tree Right Side View 🧠 Problem Return the values of nodes visible from the right side of a binary tree. 💡 Approach • Use DFS traversal • Traverse right subtree first, then left • If current level equals ans size → push node value • This ensures first node seen at each level is from right side ⏱ Time Complexity O(n) 📦 Space Complexity O(h) (recursion stack) ✅ Result: Accepted ⚡ Runtime: 2 ms #Day87 #LeetCode #BinaryTree #DFS #TreeTraversal #DSA #CodingJourney #ProblemSolving
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