#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
Binary Tree Paths via DFS Recursion
More Relevant Posts
-
#day332 of #1001daysofcode problem statement (0226): Invert Binary Tree The idea is to recursively swap the left and right child of every node in the tree. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(h) — recursion stack (h = height of tree) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
𝗬𝗼𝘂 𝗵𝗮𝘃𝗲 𝗯𝗲𝗲𝗻 𝘂𝘀𝗶𝗻𝗴 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 𝘄𝗿𝗼𝗻𝗴. It is not just for sorted arrays. The real definition — "Eliminate HALF the search space with each decision." That one shift in thinking unlocks 20+ LeetCode problems. Swipe to see the template, live code, and 7 problems you can now solve with one pattern. 👇 Save this. 🔖 #DSA #BinarySearch #Java #LeetCode #CodingInterview #DebugWithPurpose
To view or add a comment, sign in
-
Day 25/100: Finding the "Gap" 🎯 Today's challenge: Search Insert Position. We all know Binary Search finds an element in O(log n), but what if the element isn't there? I learned that by the end of the search, the `left` pointer doesn't just give up—it points exactly to where that missing number *should* be inserted to keep the array sorted. It’s a powerful way to handle dynamic data without breaking the order. Quarter of the way through the challenge! 🚀 #100DaysOfCode #Java #DSA #BinarySearch #ProblemSolving #Unit3 #Day25 #LearnInPublic
To view or add a comment, sign in
-
-
🚀 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
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
-
-
#day334 of #1001daysofcode problem statement (1009): Complement of Base 10 Integer 💡 Approach: To find the complement of a number, I converted it into binary and flipped every bit (0 → 1 and 1 → 0). After flipping the bits, the resulting binary string was converted back to decimal. Example: 5 → Binary: 101 Complement: 010 → 2 ⏱ Time Complexity: O(log n) 🧠 Space Complexity: O(log n) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 29/75 — Search Insert Position Today's problem focused on applying binary search on a sorted array. Goal: Return the index of a target value if it exists. If it does not exist, return the index where it should be inserted. Approach: • Use binary search to locate the target • If the element is not found, the left pointer represents the correct insertion index Key idea: return left Because after binary search ends, left naturally points to the position where the target should be inserted. Time Complexity: O(log n) Space Complexity: O(1) 29/75 🚀 #Day29 #DSA #BinarySearch #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 33 of #GFG60 — Staying Consistent Today’s problem: Length of Longest Cycle in a Graph 🔍 Key Concepts: • Graph traversal • Cycle detection using HashMap • Tracking visit order to compute cycle length • Efficient O(V) approach 💡 Learned how to detect cycles and calculate their length without redundant traversals. Consistency is starting to compound 📈 33 days in — no breaks. #GeeksforGeeks #DSA #CodingJourney #Java #GraphTheory #Consistency #KeepGrinding
To view or add a comment, sign in
-
-
🚀 Day 47 of #100DaysOfCode 🌱 Topic: Arrays / HashMap ✅ Problem Solved: LeetCode 260 – Single Number III 🛠 Approach: Used a HashMap to track frequency of elements. Traversed the array and stored counts of each number. Iterated through the map to find elements with frequency 1. Stored those elements in the result array. This approach is straightforward but uses extra space. #100DaysOfCode #Day47 #DSA #Arrays #HashMap #BitManipulation #LeetCode #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 35 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Valid Anagram Problem Insight: Given two strings, check if one is an anagram of the other. Approach: • First, check if the strings have the same length; if not, return false • Convert both strings to character arrays • Sort both arrays • Compare the sorted arrays — if equal, the strings are anagrams Time Complexity: • O(n log n) — due to sorting the arrays Space Complexity: • O(n) — for the character arrays Key Learnings: • Sorting is a simple and effective way to compare character compositions • Edge cases like different lengths should be handled first • Breaking the problem into small steps makes it easy to reason about Takeaway: Sometimes, sorting can reduce a seemingly complex problem into a simple comparison. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Strings
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