🔥 Day 85 of my LeetCode Journey 🔥 📘 Problem: 257. Binary Tree Paths 🎯 Difficulty: Easy 🔹 Problem Statement: Given the root of a binary tree, return all root-to-leaf paths in any order. A leaf is a node with no children. 🔹 Approach Used: Use recursion to traverse the tree If a node is a leaf → add its value as a path For non-leaf nodes: Recursively get paths from left subtree Recursively get paths from right subtree Append current node value with "->" to each child path Collect and return all paths 🔹 Key Concepts: Recursion Depth-First Search (DFS) Tree traversal String manipulation 🔹 Learning: This problem strengthens understanding of DFS traversal and how to build paths dynamically during recursion. It also shows how to combine results from subtrees efficiently. ✅ Status: Accepted (Runtime: 5 ms) 📌 Break problems into smaller subproblems — recursion does the rest 🚀 #LeetCode #Day82 #Java #BinaryTree #DFS #Recursion #DSA #ProblemSolving
LeetCode Day 85: Binary Tree Paths with DFS Recursion
More Relevant Posts
-
LeetCode Challenge – Day 51 Today I solved the Diameter of Binary Tree problem. Problem Insight: The task is to find the length of the longest path between any two nodes in a binary tree. The path does not necessarily pass through the root. Approach: Used Depth First Search (DFS) to calculate the height of each subtree For every node: Calculated left subtree height Calculated right subtree height Updated the maximum diameter as the sum of left and right heights Returned the maximum diameter found during traversal Key Learning: Tree problems often combine multiple concepts. Here, calculating height and updating diameter together in a single traversal makes the solution efficient. Complexity: Time: O(n) Space: O(h) This problem helped me understand how recursion can be used to compute multiple values in a single traversal. #LeetCode #Java #DSA #CodingJourney
To view or add a comment, sign in
-
🚀 Day 4 of My LeetCode Journey Solved today’s POTD (Problem of The Day): Closest Equal Element Queries (Medium) 📝 Problem Summary Given a circular array, for each query index, find the minimum distance to another index having the same value. If no such index exists, return -1. 🧠 Intuition Instead of checking every index for each query (brute force), I realized we only need to focus on positions where the same value occurs. 💡 Approach ✔️ Used a HashMap to store: value → list of indices ✔️ For each query: Found the index position using binary search Checked only left and right neighbors (closest possible matches) ✔️ Calculated distance using circular logic: min(|i - j|, n - |i - j|) 📚 Key Learning Smart preprocessing can reduce time complexity significantly In circular arrays, always consider wrap-around distance Nearest element problems often reduce to adjacent comparisons #LeetCode #Day4 #DSA #ProblemOfTheDay #Java #CodingJourney #PlacementPreparation
To view or add a comment, sign in
-
-
Day 4/30 — DSA Challenge 🚀 Problem: Path Sum Topic: Tree + Recursion Difficulty: Easy Approach: Used DFS to traverse all root-to-leaf paths Kept a running sum while traversing At leaf node → checked if sum equals target Mistake / Challenge: Initially confused about when to check the sum Tried checking at every node instead of only at leaf nodes Fix: Checked condition only when reaching a leaf node Simplified recursion by passing current sum Key Learning: In tree problems, clearly identify the base condition (leaf node here) Avoid unnecessary checks at intermediate nodes Time Taken: 30 minutes Consistency check ✅ See you on Day 5. GitHub Repo: https://lnkd.in/gHW9vKUf #DSA #LeetCode #Java #Trees #Recursion #LearningInPublic
To view or add a comment, sign in
-
-
Today I solved LeetCode 104 – Maximum Depth of Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Tree traversal 🧠 Approach: Use DFS (recursion) to explore the tree. For each node: Recursively calculate the depth of the left subtree. Recursively calculate the depth of the right subtree. The depth of the current node = 1 + max(left depth, right depth) Base case: if node is null, return 0. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Understanding tree depth calculation. Strengthening recursion and DFS concepts. Difference between maximum depth vs minimum depth. Building intuition for tree-based problems. Strong fundamentals in trees make advanced problems easier Consistency is the key to improvement #LeetCode #DSA #BinaryTree #MaximumDepth #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
🚀 LeetCode Daily Progress – Rotate Array Kicking off today’s DSA practice, I solved the “Rotate Array” problem on LeetCode. 🔍 Problem Overview: Given an integer array, rotate the array to the right by k steps, where k is non-negative. The rotation must be done in-place without using extra space. 💡 My Approach: Used the array reversal technique for optimal performance First reversed the initial part, then the remaining part, and finally the entire array Applied k = k % n to handle cases where k is greater than array size 📈 Key Takeaway: This problem reinforced my understanding of in-place array manipulation and how clever use of reversal algorithms can optimize both time and space complexity. Consistency is building confidence step by step 💯 #LeetCode #DSA #Java #Arrays #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency #Learning
To view or add a comment, sign in
-
-
💡 Day 61 of LeetCode Problem Solved! 🔧 🌟 482. Find Maximum Consecutive Ones 🌟 🔗 Solution Code: https://lnkd.in/gf5DPq4E 🧠 Approach: • Traverse the binary array once using one pointer. • Keep counting consecutive 1s and update the maximum value at each step. • When a 0 appears, reset the count to 0. ⚡ Key Learning: • This is a simple sliding window idea. No need for nested loops or extra memory. Tracking current count and maximum value is a useful pattern for many array problems. ⏱️ Complexity: • Time: O(N) • Space: O(1) #LeetCode #Java #DSA #ProblemSolving #Consistency #CodingJourney #Algorithms #Arrays
To view or add a comment, sign in
-
-
🚀 Day 88 / 100 — LeetCode Challenge Solved: Find Minimum in Rotated Sorted Array 💡 Approach: Used Binary Search (O(log n)) to efficiently locate the minimum element in a rotated sorted array. Instead of scanning linearly, compared mid with high to decide the search direction. 🔍 Key Insight: If nums[mid] > nums[high] → minimum is in the right half Else → minimum is in the left half (including mid) ⚡ Performance: Runtime: 0 ms (Beats 100%) Memory: 43.92 MB 🧠 Learning: Understanding how sorted + rotated arrays behave helps reduce time complexity drastically from O(n) → O(log n) Consistency is key. 12 days to go 💯🔥 #Day88 #LeetCode #BinarySearch #Java #DSA #100DaysOfCode
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 107 DSA Problem Solving LeetCode 21 – Merge Two Sorted Lists 🔥 Today I solved one of the most classic linked list problems — “Merge Two Sorted Lists”. At first glance, it looks simple, but it tests how well you understand: Pointers in Linked List Iterative traversal Edge case handling (null nodes) Clean pointer manipulation without extra space 💡 Approach Used: I used a two-pointer technique with a dummy node to efficiently merge both sorted lists while maintaining order. Instead of creating new nodes, I focused on: 👉 Re-linking existing nodes 👉 Maintaining a tail pointer 👉 Handling remaining nodes carefully ⚙️ Time Complexity: O(n + m) ⚙️ Space Complexity: O(1) 🚀 Key Learning: Small pointer mistakes can break the whole structure — but once logic is clear, linked lists become powerful and elegant. This problem strengthened my fundamentals in linked list manipulation and boosted my confidence for more complex problems. #DSA #LinkedList #LeetCode #ProblemSolving #CodingJourney #Java
To view or add a comment, sign in
-
-
🚀 Day 27/100 – LeetCode DSA Challenge 📌 Problem Solved: Find All Numbers Disappeared in an Array Today’s problem focused on identifying missing numbers from an array where elements are in the range [1, n]. 🔍 Approach I Used (Easy Method): Created a boolean array to track visited numbers Marked each number as present Iterated from 1 to n to find missing elements 💡 Key Concepts Learned: How to use index-based mapping for arrays Importance of range constraints [1, n] Difference between brute force vs optimized approach Using extra space to simplify logic 🧠 What I Understood Today: If a problem says numbers are in range [1, n], we can directly map values to indices Tracking frequency or presence is a powerful technique Writing clean and simple logic first is more important than jumping to optimization 🔥 Next Step: I will try the optimized approach (O(1) space) using index marking (negative marking technique) 📈 Improving step by step in DSA — consistency is the key! #Day27 #LeetCode #DSA #100DaysOfCode #CodingJourney #PlacementsPreparation #Java
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