🚀 DSA Consistency - Day 57 Today I solved the Same Tree problem on LeetCode, which focuses on understanding binary tree structure comparison using recursion. The goal is to determine whether two binary trees are structurally identical and have the same node values. 🧠 Approach: Recursive Tree Traversal To check if two trees are the same: 1️⃣ If both nodes are null, they are identical → return true. 2️⃣ If one node is null and the other is not, trees differ → return false. 3️⃣ If node values are different, trees are not identical. 4️⃣ Recursively check: Left subtree of both trees Right subtree of both trees Both must match for the trees to be identical. ⏱ Complexity Analysis Time Complexity: O(n) Each node is visited once. Space Complexity: O(h) Due to recursion stack (where h is the height of the tree). #DSA #LeetCode #BinaryTree #Java #CodingJourney #Consistency #ProblemSolving #100DaysOfCode
Same Tree Problem Solved on LeetCode
More Relevant Posts
-
Day 41/100 | #100DaysOfDSA 💻🚀 Today’s problem: Find the Duplicate Number Goal: Given an array with n + 1 numbers where each number is in the range [1, n], find the duplicate number without modifying the array and using constant extra space. Approach: Binary Search on Answer Idea: • The duplicate must lie in the range 1 → n • Count how many numbers are ≤ mid • If the count is greater than mid → duplicate is in the left half • Otherwise search the right half Key insight: Using the pigeonhole principle, if more numbers fall in a range than expected, a duplicate must exist there. Time Complexity: O(n log n) Space Complexity: O(1) Big takeaway: Binary search isn’t just for sorted arrays — it can also be applied on the range of possible answers. Day 41 — patterns getting clearer. 🔥 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Java #CodingJourney #InterviewPrep #ProblemSolving #TechCommunity
To view or add a comment, sign in
-
-
#day333 of #1001daysofcode problem statement (0257): Binary Tree Paths 💡 Approach: Used DFS recursion to explore all root-to-leaf paths in the binary tree. While traversing, I kept building the path string. When a leaf node is reached, the complete path is added to the result list. Example path format: 1->2->5 ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(h) — recursion stack (h = height of the tree) Consistency in solving one problem every day 📈 #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 16/100 – LeetCode Challenge Problem: Merge Sorted Array Today’s problem involved merging two sorted arrays into one sorted array. Approach: Created a temporary array of size m + n Used two pointers to compare elements from both arrays Inserted the smaller element into the new array Copied remaining elements if any array still had values Finally copied the merged result back into nums1 Complexity: Time: O(m + n) Space: O(m + n) Concepts Practiced: Two-pointer technique Array traversal Merging sorted arrays #100DaysOfCode #LeetCode #DSA #Java #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 61/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Median of Two Sorted Arrays A classic hard problem with a clever binary search approach. Problem idea: Find the median of two sorted arrays without fully merging them. Key idea: Use binary search on the smaller array to partition both arrays. Why? • We want left half and right half to be balanced • All elements in left ≤ all elements in right How it works: • Pick a cut in array1 • Derive cut in array2 • Check partition validity using boundary elements If valid → we found the median If not → adjust the partition Time Complexity: O(log(min(m, n))) Space Complexity: O(1) Big takeaway: Binary search isn’t just for searching — it can be used to optimize partitions and positions. This one really builds intuition. 🔥 Day 61 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 68 of My DSA Journey Today’s problem: Symmetric Tree 🌳 Problem Statement Given a binary tree, check whether it is a mirror of itself (symmetric around its center). Key Insight A tree is symmetric if: Left subtree is a mirror of the right subtree Compare nodes in a cross manner: Left → Left with Right → Right Left → Right with Right → Left 🧠 Approach Use recursion to compare two nodes at a time Base cases: If both nodes are null → symmetric If one is null → not symmetric Check: Values are equal Outer and inner pairs match ⚡ Complexity Time: O(n) Space: O(h) (recursion stack) ✨ What I Learned This problem improved my understanding of recursion and how to think in terms of mirror structures instead of normal traversal. Consistency is the key 🔑 — one problem at a time! #DSA #Java #BinaryTree #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 62 of #100DaysOfCode 🌱 Topic: Trees / Recursion ✅ Problem Solved: LeetCode 112 – Path Sum 🛠 Approach: Used DFS (recursion) to explore all root-to-leaf paths. Base Case: If node is null → return false If leaf node: Check if target == node.val → return result Otherwise: Reduce target → target - node.val Recurse on left and right subtree #100DaysOfCode #Day62 #DSA #Trees #Recursion #DFS #LeetCode #Java #BinaryTree #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 50 / 100 | Median of Two Sorted Arrays Intuition: We are given two sorted arrays and need to find the median of the combined numbers. Since both arrays are already sorted, we can merge them in sorted order. Once we have the merged array, finding the median becomes simple. If the total number of elements is odd, the median is the middle element. If it's even, the median is the average of the two middle elements. Approach: Use two pointers to traverse both arrays. Compare the elements and insert the smaller one into a new array. Continue this process until all elements are merged. Finally, calculate the median based on the length of the merged array. Complexity: Time Complexity: O(n + m) Space Complexity: O(n + m) #100DaysOfCode #Java #DSA #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 47 🔍 Solved: Find Minimum in Rotated Sorted Array Today I explored another interesting variation of Binary Search. Instead of searching for a target, the goal was to find the minimum element in a rotated sorted array. 💡 Key Insight: By comparing the middle element with the last element, we can determine which half contains the minimum value. Approach: ✔ Used Binary Search to achieve O(log n) time complexity ✔ Compared mid with end to identify the unsorted portion ✔ Narrowed down the search space efficiently What I Learned: This problem helped me understand how binary search can be applied beyond simple searching—especially in rotated and partially sorted arrays. #Java #DSA #LeetCode #BinarySearch #CodingJourney #ProblemSolving #TechSkills
To view or add a comment, sign in
-
-
Solved a matrix problem : checking whether one matrix can be obtained from another using rotations. At first it looked a bit tricky, but the key idea was simple: 👉 A 90° rotation can be done using transpose + reverse each row This makes the solution clean and efficient (O(n²)) and is a useful pattern to remember for similar problems. #DataStructures #Algorithms #DSA #Java #LeetCode #ProblemSolving #SoftwareEngineering
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