Day 72 | #100DaysOfCode | #100DaysOfLearning 🧿 Continuing the Binary Tree pattern ✅ Solved: Same Tree (Leetcode 100) Approach: • If both nodes are NULL → trees are same • If one is NULL → not same • If values are equal → recursively check left & right subtrees This problem helped me clearly understand how recursion compares two trees simultaneously. 🧠 What I learned: ✔️ Structural comparison of trees ✔️ Writing clean recursive base cases ✔️ Thinking in terms of subtree matching Trees are becoming more intuitive step by step Consistency continues. #DSA #BinaryTree #Recursion #TreeProblems #Leetcode #ProblemSolving #CodingJourney #Consistency
Binary Tree Comparison with Recursion
More Relevant Posts
-
#100DaysLeetCode Day 35 ✅ Solved LeetCode 523: Continuous Subarray Sum Given an array nums and an integer k, the goal is to check if there exists a subarray of size ≥ 2 whose sum is a multiple of k. 🚀 Approach Used Prefix Sum + HashMap (modulo trick) Key idea: If two prefix sums have the same remainder when divided by k, then the subarray between them is divisible by k Steps: Maintain a running sum Store (sum % k) in a hashmap with index If same remainder appears again → check distance ≥ 2 Also handled edge case: When k == 0, avoid modulo and handle separately ⏱ Complexity Time Complexity: O(n) Space Complexity: O(n) #100DaysLeetCode #Day35 #LeetCode523 #leetcode #cpp #dsa #arrays #hashmap #prefixsum #problemSolving #coding #interviewprep
To view or add a comment, sign in
-
-
🚀 Day 40/100 – #100DaysOfLeetCode Keeping the streak alive with consistency and problem-solving energy! ✅ Problem Solved: Add Binary (LeetCode 67) 🧠 Problem Statement: Given two binary strings a and b, return their sum as a binary string. 💡 Approach Used: 🔹 Right-to-Left Traversal: Started from the last digit of both binary strings. 🔹 Carry Management: Added corresponding bits along with the carry from the previous step. 🔹 Binary Addition Logic: Calculated the result bit using sum % 2 and updated the carry using sum / 2. 🔹 Result Construction: Appended each computed bit and finally reversed the string to get the correct order. 📌 Edge Case Handling: • When the two strings have different lengths. • When an extra carry remains after processing all digits. • Handling cases where one string finishes earlier than the other. 📊 Time Complexity: O(n) — where n is the maximum length of the two binary strings. 🗂 Space Complexity: O(n) — for storing the resulting binary string. ✔️ Status: Accepted 🎉 📍 Learning: This problem strengthens the understanding of binary arithmetic and string manipulation. It also demonstrates how simple mathematical operations can simulate binary addition efficiently without converting the strings into integers. 🎯 39/100 complete. Small daily progress leads to big achievements over time 🚀 #LeetCode #DSA #CodingJourney #ProblemSolving #Consistency #100DaysOfCode #Cpp #Algorithms #Binary #NavneetRaj
To view or add a comment, sign in
-
-
🚀 LeetCode 1758 — Minimum Changes To Make Alternating Binary String Today’s problem focuses on recognizing patterns in binary strings. 🔹 A binary string is alternating if no two adjacent characters are the same. Valid patterns can only be: 010101... 101010... 💡 Key Insight Instead of constructing both patterns, we can compare the string with just one pattern (0101...) and count mismatches. "01"[i & 1] helps generate the expected character at index i. ops → number of changes required to convert the string to 0101... n - ops → changes required to convert it to 1010... The answer is simply the minimum of these two values. ⚡ Time Complexity: O(n) 💾 Space Complexity: O(1) 🧠 Idea: Count mismatches with one alternating pattern and derive the other automatically. #LeetCode #DSA #Algorithms #BinaryString #CodingPractice #ProblemSolving #CompetitiveProgramming
To view or add a comment, sign in
-
-
🔥 Day 163 of My LeetCode Journey Problem 22: Generate Parentheses 💡 Problem Insight: Today’s problem was about generating all valid combinations of n pairs of parentheses. This isn’t about generating all strings it’s about building only valid ones from the start. 🧠 Concept Highlight: The solution uses backtracking with constraints: Add '(' if you still have openings left Add ')' only if it won’t break validity (closing ≤ opening) Explore all valid paths and backtrack cleanly This avoids generating invalid combinations and keeps the search space controlled. 💪 Key Takeaway: Don’t generate everything and filter later. Build only valid states — that’s the difference between brute force and smart recursion. ✨ Daily Reflection: This problem reinforces that recursion is about decision trees and constraints, not just function calls. Once you visualize the tree, the logic becomes clear. #Day163 #LeetCode #Backtracking #GenerateParentheses #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 83 of #100DaysOfCode Today I solved "Binary Tree Maximum Path Sum" on LeetCode using DFS + Recursion. Key Idea: At every node, we calculate the maximum path sum passing through it. But here’s the twist We ignore negative paths because they reduce the total sum. Approach: • Recursively get the max path sum from left and right subtrees • Ignore negative values using max(0, …) • Update the global answer as: root->val + left + right • Return to parent: root->val + max(left, right) This ensures we consider both: Path passing through the node Path extending upwards Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Greedy choice (ignore negative paths) Time Complexity: O(n) Space Complexity: O(h) This problem really sharpened my understanding of handling multiple path possibilities in trees From simple traversals to advanced patterns — growth is visible #Day83 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney
To view or add a comment, sign in
-
-
Day 16 of #30DaysOfLeetCode Solved LeetCode Problem #69 – Sqrt(x) using C. Problem: Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The solution must not use built-in functions like sqrt() or pow(). Approach I used: • Applied Binary Search to find the integer square root • Searched within the range 0 to x • Compared mid * mid with x • If it was smaller → moved right • If it was larger → moved left • Stored the last valid value as the final answer Performance: • Runtime: 0 ms • Memory: 9.06 MB What I learned today: • How binary search can be used for mathematical problems • Importance of choosing the correct data type (long) to avoid overflow • Better understanding of efficient problem-solving techniques • Staying consistent every day is making the problems easier to solve #LeetCode #DSA #Algorithms #BinarySearch #CProgramming #ProblemSolving
To view or add a comment, sign in
-
-
#100DaysLeetCode Day 32 ✅ Solved LeetCode 169: Majority Element The task is to find the element that appears more than ⌊n/2⌋ times in the array. 🚀 Approach Used the Boyer–Moore Voting Algorithm. Idea: Maintain a candidate (majority element) and a count. If count becomes 0, update the candidate. If current element equals candidate → increase count. Otherwise → decrease count. Because the majority element appears more than half of the time, it will remain the final candidate. Time Complexity: O(n) Space Complexity: O(1) #100DaysLeetCode #Day32 #LeetCode169 #leetcode #cpp #dsa #arrays #algorithm #problemSolving #coding #interviewprep
To view or add a comment, sign in
-
-
🚀 Day 22/50 – LeetCode Challenge 🧩 Problem: Symmetric Tree Today’s problem focused on checking whether a binary tree is a mirror of itself — a great exercise in recursion and tree traversal. 📌 Problem Summary: Given the root of a binary tree, determine whether it is symmetric around its center. A tree is symmetric if the left subtree is a mirror reflection of the right subtree. 🔍 Approach Used ✔ Used recursion to compare nodes in a mirrored way ✔ Checked: If both nodes are null → symmetric If one is null → not symmetric If values are equal → continue checking ✔ Compared: left subtree of one side with right subtree of the other right subtree with left subtree ⏱ Time Complexity: O(n) 📦 Space Complexity: O(h) 💡 Key Learning ✔ Understanding mirror structure in trees ✔ Applying recursion with multiple conditions ✔ Importance of checking both sides (left ↔ right and right ↔ left) ✔ Strengthening binary tree problem-solving This problem showed how small logical mistakes (like missing one recursive check) can affect correctness — a great learning experience. Consistency builds mastery 🚀 🔗 Problem Link: https://lnkd.in/gSHvYR2S #50DaysOfLeetCode #LeetCode #DSA #BinaryTree #Recursion #ProblemSolving #CodingJourney #FutureAIEngineer #Consistency
To view or add a comment, sign in
-
-
Day 28 of #100DaysOfLeetCode Problem: Subarray Sum Equals K Difficulty: Medium Key Concept Learned: Prefix Sum with HashMap Today I solved the problem Subarray Sum Equals K. The goal was to count how many contiguous subarrays have a sum equal to k. At first it might seem like we need to check every possible subarray, but that would be too slow. Instead, I used the Prefix Sum pattern. While traversing the array, I kept track of the running sum of elements. For every position, I checked whether there was a previous prefix sum such that the difference between the current prefix and that previous prefix equals k. To do this efficiently, I used a hashmap to store how many times each prefix sum had appeared so far. If the required prefix already exists in the map, it means we found a subarray whose sum is equal to k, so we increase the count. This allows us to solve the entire problem in a single pass through the array. Time Complexity: O(n) Space Complexity: O(n) Key Takeaways Prefix Sum is a powerful pattern for solving subarray problems Using a hashmap helps store previous sums and makes the lookup fast Many problems that look like O(n²) can often be optimized to O(n) Understanding patterns makes solving medium problems much easier Slowly getting better at recognizing these patterns while solving problems. Proud and grateful for the continuous guidance from Pratyush Narain. #100DaysOfLeetCode #DSA #LeetCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 29 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Reverse a String We were given a string. Task was simple. Reverse the characters. Example: "hello" → "olleh" Looks easy. But there are multiple ways to do it. 💻 Brute Force Approach 🔹️Create a new empty string. 🔹️Traverse the original string from the end. 🔹️Append each character to the new string. 🔹️Return the new reversed string. Time Complexity: O(n) Space Complexity: O(n) Because a new string is created. 💻 Optimal Approach (Two Pointers) 🔹️Convert the string into a character array. 🔹️Place one pointer at the start. 🔹️Place another pointer at the end. 🔹️Swap both characters. 🔹️Move pointers toward the center. Continue until they meet. Time Complexity: O(n) Space Complexity: O(1) No extra string needed. 📚 What I learned today: ▫️Two-pointer technique works well for reversal problems. ▫️In-place operations help reduce space complexity. ▫️String problems often become easier when treated as arrays. ▫️Pointer movement patterns appear in many algorithms. Day 29 completed. Small problem, but useful pattern 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
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