Day 36/100 | #100DaysOfDSA 🌳💻 Today’s problem: Subtree of Another Tree The task is to determine whether one binary tree is a subtree of another. A subtree must match both the structure and node values of the original tree. Approach: • Traverse the main tree using recursion • At each node, check if the subtree rooted there matches the given subRoot • Use a helper function to compare both trees for structural and value equality • If no match is found, continue checking the left and right subtrees This works by combining two recursive checks: 1️⃣ Search for a potential matching root 2️⃣ Verify if both trees are identical Time Complexity: O(n × m) Space Complexity: O(h) Big takeaway: Complex tree problems often break down into combining simpler tree problems — in this case, tree traversal + same tree comparison. Building stronger tree problem-solving patterns every day. 🚀 #100DaysOfCode #LeetCode #DSA #BinaryTree #Trees #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity #SoftwareEngineer #Consistency #Programmers #TechGrowth
Yogesh ..’s Post
More Relevant Posts
-
LeetCode Problem #1920 – Build Array from Permutation Today I solved an interesting array problem that helped me understand indexing and array traversal better. 📌 Problem: Given a 0-indexed array `nums`, build a new array `ans` such that: `ans[i] = nums[nums[i]]` 📌 Approach: * Create a new array `ans` with the same length as `nums` * Traverse the array using a `for` loop * For each index `i`, access the value `nums[i]` * Use that value as an index again to get `nums[nums[i]]` * Store the result in `ans[i]` 📌 Key Concepts Practiced: ✔ Array traversal ✔ Nested indexing ✔ Understanding how values can act as indexes 📌 Time Complexity: O(n) – since we traverse the array only once. 📌 Key Takeaway: Sometimes the value inside an array can also represent an index, and understanding this idea helps solve many array-based problems efficiently. 💡 Learning Note This problem improved my understanding of **index-based operations and array manipulation**, which are fundamental concepts for solving many coding interview questions. #LeetCode #CodingPractice #Java #DSA #Arrays #ProblemSolving
To view or add a comment, sign in
-
-
Day 35/100 | #100DaysOfDSA 🌳💻 Today’s problem: Same Tree The goal is to determine whether two binary trees are identical — meaning they have the same structure and the same node values. Approach: • Use recursion to traverse both trees simultaneously • If both nodes are null → they match • If one node is null and the other isn’t → trees are different • If node values differ → trees are different • Recursively check the left and right subtrees Both trees must match at every node for them to be considered the same. Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Big takeaway: When comparing two trees, traverse them in parallel and validate structure and values at each step. Simple logic, but a great exercise for strengthening recursive tree thinking. One more tree pattern learned. 🚀 #100DaysOfCode #LeetCode #DSA #BinaryTree #Trees #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity #SoftwareEngineer #Consistency #Programmers #TechGrowth
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟑𝟑 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on generating all unique subsets when the array contains duplicate elements. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Subsets II 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • First, sorted the array to group duplicate elements together • Used backtracking to explore subset possibilities • At each step, two choices exist: include the element or skip it • When skipping, moved forward past duplicate elements to avoid repeated subsets This ensures unique subsets are generated. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Sorting helps handle duplicates effectively • Backtracking explores all combinations systematically • Skipping duplicate elements prevents repeated subsets • Understanding recursion trees helps visualize subset generation 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n × 2ⁿ) • Space: O(n) (recursion stack) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 When duplicates exist, generating combinations isn’t the challenge — avoiding repeated ones is. 33 days consistent 🚀 On to Day 34. #DSA #Arrays #Backtracking #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
Day 45/100 | #100DaysOfDSA 🚀🧩 Today’s problem: Remove All Occurrences of a Substring The task is simple: Given a string s and a substring part, keep removing the leftmost occurrence of part until it no longer exists in s. Approach: • Use StringBuilder for efficient modifications • Find the index of part using indexOf() • Delete that portion from the string • Repeat the process until part is no longer found Time Complexity: O(n × k) in worst case (where n is length of string and k is length of substring) Big takeaway: Sometimes the easiest solution is just repeating a simple operation until the condition disappears. Clean string manipulation problem today. 🔥 Day 45 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Java #String #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 62/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Combination Sum Classic backtracking problem. Problem idea: Find all unique combinations where numbers sum up to a target. Each number can be used unlimited times. Key idea: Use backtracking (choose → explore → backtrack). Why? • We need to explore all possible combinations • Stop when sum exceeds target • Add result when sum == target How it works: • Pick a number • Stay on same index (reuse allowed) • Move forward when skipping • Backtrack to try other paths Time Complexity: Exponential (depends on combinations) Space Complexity: O(target) recursion depth Big takeaway: Backtracking is all about exploring choices and undoing them efficiently. This pattern is super important for recursion problems. 🔥 Day 62 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Backtracking #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 37 of #75DaysofLeetCode 🚀 LeetCode 1372 – Longest ZigZag Path in a Binary Tree Just solved an interesting tree problem that really tests your understanding of DFS + state tracking 🌳 🔍 Problem Insight: A ZigZag path alternates between left ↔ right at every step. The goal is to find the longest such path in a binary tree. 💡 Key Idea: At every node, we track: Current direction (left or right) Current path length We have two choices: Continue the ZigZag → increase length Restart from opposite direction → reset length 🧠 Approach (DFS): Use recursion to explore both directions Keep updating a global maximum Try starting from both left and right 📊 Complexity: Time: O(N) Space: O(H) (recursion stack) 🔥 Takeaway: This problem is a great example of how tracking state in recursion can simplify complex tree traversals. #LeetCode #DataStructures #BinaryTree #DSA #CodingInterview #Java #100DaysOfCode
To view or add a comment, sign in
-
-
Day 18 – Same Tree 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem focused on checking whether two binary trees are identical, which is a classic tree recursion problem. Approach We compare both trees node by node using recursion. At each step: 1️⃣ If both nodes are null → trees match 2️⃣ If one is null → not same 3️⃣ If values differ → not same 4️⃣ Recursively check left and right subtrees Time Complexity : O(N). Space Complexity : O(N) . Github Code :- https://lnkd.in/guF5Vq9m #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #Recursion #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day18 #DeveloperJourney
To view or add a comment, sign in
-
-
Day 15 – Maximum Depth of Binary Tree 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem focused on finding the maximum depth (height) of a binary tree, which is a fundamental concept used in many tree-related problems. Approach We use recursion (DFS) to compute the height of the tree. For every node: Height = 1 + max(height of left subtree, height of right subtree) Base case: If node == null → return 0. Time complexity : O(N) . Space Complexity : O(N). Github Code :- https://lnkd.in/gnDkqcFQ #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #TreeRecursion #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day15 #DeveloperJourney
To view or add a comment, sign in
-
-
🚀 Day 29 of #75daysofLeetCode 2095 – Delete the Middle Node of a Linked List Just solved an interesting linked list problem that perfectly demonstrates the power of the two-pointer technique (slow & fast pointers). 🔍 Problem Insight: Given a linked list, delete its middle node where the middle is defined as ⌊n/2⌋ (0-based indexing). 💡 Key Idea: Instead of calculating the length, we can efficiently find the middle using: 🐢 Slow pointer (1 step) ⚡ Fast pointer (2 steps) When the fast pointer reaches the end, the slow pointer will be at the middle node! 🛠 Approach: ✔ Handle edge case (single node → return null) ✔ Traverse using slow & fast pointers ✔ Keep track of previous node ✔ Remove the middle node in one pass ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🔥 Why this matters? This pattern is widely used in: Finding middle of linked list Detecting cycles Splitting lists Mastering this unlocks many problems! #LeetCode #DSA #LinkedList #Java #CodingInterview #ProblemSolving #TechLearning
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟑 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding the maximum product subarray. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Maximum Product Subarray 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Tracked two values at each step: • Current maximum product • Current minimum product • Why both? • A negative number can turn a small product into a large one • For each element: • Calculated new max and min using previous values • Updated the result with the maximum product found 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Negative numbers can flip the result completely • Tracking both max and min is crucial • DP can be optimized using variables instead of arrays • Edge cases (like zero) reset the product 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the minimum value is just as important as the maximum — because it might become the next maximum. 53 days consistent 🚀 On to Day 54. #DSA #Arrays #DynamicProgramming #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
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