🚀 Just Solved LeetCode #1367 — Linked List in Binary Tree 📘 Problem: Given the head of a linked list and the root of a binary tree, determine whether all the elements of the linked list appear as a downward path in the binary tree. Downward means starting from any node and moving only to child nodes. Example: Input → head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] Output → true Explanation → Nodes in blue form a subpath in the binary tree. 🧠 My Approach: I used recursion to check whether the linked list sequence can be found starting from any node in the binary tree. 1️⃣ For each node in the tree, check if the list starting from `head` matches the downward path from that node. 2️⃣ If not, recursively check the left and right subtrees. 3️⃣ Used a helper function `checkPath()` to verify if a valid path continues as the recursion goes deeper. 💡 What I Learned: ✅ How to combine linked list and binary tree traversal logic ✅ How recursion can efficiently check multiple starting points ✅ Improved my understanding of tree path traversal and backtracking logic #LeetCode #Java #DSA #BinaryTree #LinkedList #CodingUpdate #LearningByDoing
Solved LeetCode #1367: Linked List in Binary Tree with Recursion
More Relevant Posts
-
🔢 Day 47 of #LeetCode100DaysChallenge Solved LeetCode 79: Word Search — a classic backtracking problem that beautifully blends recursion and grid traversal. 🧩 Problem: Given a 2D grid of characters and a word, determine if the word can be formed using sequentially adjacent cells (up, down, left, right), where each cell can be used only once. 💡 Approach Used — Backtracking (DFS): 1️⃣ Start from each cell that matches the first character. 2️⃣ Explore in all four directions recursively. 3️⃣ Temporarily mark visited cells to avoid reuse. 4️⃣ If the entire word is matched, return true; otherwise, backtrack. ⚙️ Complexity: Time: O(N × 4ᴸ) — where N is the total number of cells, and L is the word length. Space: O(L) — recursion depth. ✨ Key Takeaways: ✅ Strengthened understanding of recursion and backtracking. ✅ Learned to manage visited states effectively in grid problems. ✅ Great exercise in applying DFS to real-world matrix traversal cases. #LeetCode #100DaysOfCode #Java #Backtracking #Recursion #ProblemSolving #DSA #WomenInTech #CodingJourney
To view or add a comment, sign in
-
-
🚀 LeetCode Progress Update: Invert Binary Tree (Problem 226) 🌳 About the Problem: The task was to invert a binary tree — flipping it by swapping every left and right child node. It’s a classic problem that tests recursion and tree traversal logic. 🧠 My Approach: I went with a recursive approach. At each node, I swapped the left and right subtrees, then called the same logic for both sides. Once the node became null, the recursion stopped automatically. 💡 What I Learned: ✅ How recursion travels through a tree structure ✅ Why defining a clear base condition matters ✅ How simple logic can transform an entire data structure 🎯 Takeaway: This problem reinforced that not every challenge needs complexity — sometimes, the cleanest recursive idea is the smartest one. #LeetCode #Java #CodingJourney #ProblemSolving #DSA
To view or add a comment, sign in
-
-
✅Day 50 : Leetcode 1283 - Find the Smallest Divisor Given a Threshold #60DayOfLeetcodeChallenge 🧩 Problem Statement: Given an array of integers nums and an integer threshold, find the smallest positive integer divisor such that the sum of each element divided by the divisor (rounded up to the nearest integer) is less than or equal to the threshold. 💡 My Approach: Binary Search on Divisor Range: The smallest possible divisor is 1. The largest possible divisor is the maximum element in nums. Check Function: For a given divisor, calculate the sum of ceil(nums[i] / divisor) for all elements. If the total sum ≤ threshold → we can try smaller divisors. Otherwise, increase the divisor. Repeat until optimal divisor is found. ⏱️ Time Complexity: O(n * log(max(nums))) Each binary search iteration takes O(n) to compute the sum. #Algorithms #BinarySearch #LeetCode #Java #ProblemSolving #DSA #100DaysOfCode #CodeNewbie
To view or add a comment, sign in
-
-
✨ LeetCode 104 – Maximum Depth of Binary Tree Today I solved another interesting Binary Tree problem 🌳 🧩 Problem Summary: Given the root of a binary tree, we need to find its maximum depth — the number of nodes along the longest path from the root down to the farthest leaf. 💡 Intuition: We can use recursion — At each node, the maximum depth is 1 + max(depth of left subtree, depth of right subtree). If the node is null, the depth is 0. 🧠 Approach: ✔️ Use a simple DFS traversal ✔️ Recursively calculate left and right subtree depths ✔️ Add 1 for the current node 🕒 Complexity: Time: O(N) — visit each node once Space: O(H) — recursion stack (H = height of the tree) #LeetCode #Java #DSA #BinaryTree #Recursion #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
📌 Day 27/100 - Generate Parentheses (LeetCode 22) 🔹 Problem: Given n pairs of parentheses, generate all combinations of well-formed (balanced) parentheses. 🔹 Approach: Use backtracking to build strings character-by-character. Keep two counters: open = number of '(' used, close = number of ')' used. You may add '(' while open < n. You may add ')' while close < open (to maintain balance). When the current string length reaches 2*n, add it to the result list. 🔹 Complexity: Time: proportional to the number of valid combinations (Catalan number) — roughly O(4ⁿ / n^(3/2)). Space: output-sensitive — O(n * Cn) for storing results, and O(n) extra for recursion depth. 🔹 Key Learning: Backtracking is ideal when you need to enumerate valid combinations subject to constraints. Always enforce constraints early (here: close < open) to prune invalid branches. Think in terms of state (open/close counts) rather than raw string manipulation. #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #110 — Balanced Binary Tree 📘 Problem: Given the root of a binary tree, determine if it is height-balanced — that is, for every node, the height difference between the left and right subtree should not exceed 1. Example: Input → [3,9,20,null,null,15,7] Output → true 🧠 My Approach: I used a bottom-up recursive approach to check balance efficiently. 1️⃣ For each node, I calculated the height of its left and right subtrees. 2️⃣ If any subtree was unbalanced, I returned -1 immediately to stop further checks. 3️⃣ If the height difference between left and right subtrees was greater than 1, I marked the tree as unbalanced. 4️⃣ Otherwise, I returned the height of the current node as 1 + max(left, right). This approach ensures every node is visited only once — making it O(n) in time complexity. 💡 What I Learned: ✅ The difference between top-down and bottom-up recursion in tree problems ✅ How to optimize recursion by early termination when imbalance is detected ✅ Strengthened my understanding of recursive depth and height calculation in binary trees #LeetCode #Java #DSA #BinaryTree #CodingJourney
To view or add a comment, sign in
-
-
✅Day 44 : Leetcode 162 - Find Peak Element #60DayOfLeetcodeChallenge 🧩 Problem Statement: You are given a 0-indexed integer array nums. A peak element is an element that is strictly greater than its neighbors. Your task is to find a peak element and return its index. If the array contains multiple peaks, return the index of any one of them. You must solve this in O(log n) time complexity. 💡 My Approach: I used the Binary Search approach to solve this problem efficiently. Check for edge cases: If there’s only one element, return index 0. If the first element is greater than the second, it’s a peak → return 0. If the last element is greater than the second last, return n-1. Apply binary search between indices 1 and n-2: Find the middle index mid. If nums[mid] is greater than both its neighbors (nums[mid-1] and nums[mid+1]), we found the peak → return mid. If the left neighbor is greater, move the search to the left half. Otherwise, move to the right half. This guarantees logarithmic search efficiency. ⏱️ Time Complexity: O(log n) — due to binary search. 💾 Space Complexity: O(1) — constant extra space. #BinarySearch #LeetCode #FindPeakElement #DSA #Java #CodingPractice #ProblemSolving #LogN
To view or add a comment, sign in
-
-
✅ Just solved LeetCode #111 — Minimum Depth of Binary Tree 📘 Problem: Given the root of a binary tree, find its minimum depth — the shortest path from the root node down to the nearest leaf node. A leaf node is one that has no children. Example: Input → [3,9,20,null,null,15,7] Output → 2 Explanation → The shortest path is 3 → 20 → 7. 🧠 My Approach: I used a simple recursive DFS approach to calculate the minimum depth. 1️⃣ If the node is null, return 0. 2️⃣ Recursively find the left and right subtree depths. 3️⃣ If one of the subtrees is missing, return the depth of the existing one. 4️⃣ Otherwise, return the smaller of the two depths + 1. 💡 What I Learned: ✅ The difference between height and depth in trees ✅ How to handle null nodes properly in recursion ✅ The importance of handling edge cases when one subtree is missing 💻 Language Used: Java 🔍 Approach Type: Recursive DFS #LeetCode #Java #DSA #BinaryTree #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
-
✅ Day 76 of LeetCode Medium/Hard Edition Today’s challenge was “Linked List in Binary Tree” — an elegant recursion and tree-traversal problem that blends linked list navigation with depth-first search on a binary tree 🌳🔗⚡💡 📦 Problem: You're given the head of a linked list and the root of a binary tree. Your task is to check whether the entire linked list appears as a downward path in the binary tree. A downward path means you may start at any node, but can only move to left or right children in each step. 🔗 Problem Link: https://lnkd.in/gwhvXVK4 ✅ My Submission: https://lnkd.in/gjqiBfJG 💡 Thought Process: A naive approach would try to follow the linked list from every tree node using repeated traversal — but without careful handling, this leads to redundant work. The optimized idea is to use DFS with two layers: 1️⃣ Search layer → Try starting the match from every node in the tree 2️⃣ Match layer → Check if the linked list matches a downward path from that node We define two functions: helper() → explores the tree to find a potential starting point match() → attempts to follow the linked list downward If at any node the values match and the entire list gets consumed, we return true. This structure avoids unnecessary recomputation and ensures clean, efficient traversal. ⚙️ Complexity: ⏱ Time: O(N × M) in the worst case (N = tree nodes, M = list length) 💾 Space: O(H) due to recursion stack (H = tree height) #LeetCodeMediumHardEdition #100DaysChallenge #DFS #BinaryTree #LinkedList #Java #DSA #ProblemSolving #LearnEveryday
To view or add a comment, sign in
-
More from this author
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