🧠 LeetCode Day 97 — Path Sum (Problem 112) Today’s challenge was about checking whether a binary tree has a root-to-leaf path such that adding up all the values along the path equals a given sum. 🔍 Problem Description: Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that the sum of the node values equals targetSum. Otherwise, return false. 💡 Approach: Traverse the tree recursively. Subtract the current node’s value from targetSum. When a leaf node is reached, check if the remaining sum equals zero. Use Depth First Search (DFS) to explore all paths. 🚀 Key Learnings: Strengthened understanding of recursive tree traversal. Practiced handling base cases in recursion effectively. Improved clarity on root-to-leaf path logic in binary trees. 🌳 #Day97 of #100DaysOfCode Each challenge adds a new layer to my problem-solving skills. Binary tree problems like this sharpen the recursive mindset — thinking from root to leaf and back up! 💪 #LeetCode #CodingChallenge #Java #100DaysOfCode #BinaryTree #ProblemSolving #Recursion
LeetCode Day 97: Path Sum in Binary Tree
More Relevant Posts
-
🌳 Day 60 of 100: Maximum Depth of Binary Tree 🌳 Today’s challenge was LeetCode 104 – Maximum Depth of Binary Tree 🌲 The task? Given the root of a binary tree, find the maximum depth — the number of nodes along the longest path from the root to a leaf node 🍃 💡 Intuition: A binary tree’s depth can be thought of as its “height”. To find it, we just need to know the depth of the left and right subtrees — and the answer is the greater of the two, plus one for the current node. This is a classic example of recursion done right — breaking a big problem into smaller ones that mirror the original. ⏱ Time Complexity: O(n) – visit every node once 🗂 Space Complexity: O(h) – h is the height of the tree (recursion stack) ✨ Key Takeaway: This problem beautifully highlights the power of divide and conquer — by solving smaller subproblems (left and right trees), we can elegantly solve the bigger one. #100DaysOfCode #Day60 #LeetCode #Java #CodingJourney #BinaryTree #Recursion #DataStructures #CodingPractice #ProblemSolving #WomenWhoCode #CodeNewbie #LearnToCode
To view or add a comment, sign in
-
-
🌟 Day 56 of My #LeetCode Challenge – Word Pattern (#290) Today’s problem focused on validating if a string follows a specific character pattern — a great exercise in understanding mapping relationships and string manipulation. The task was to ensure that: 1️⃣ Each character in the pattern maps to exactly one word in the string. 2️⃣ No two characters map to the same word — maintaining a perfect one-to-one (bijection) relationship. To solve this, I used a HashMap<Character, String> to store the mappings between each character and its corresponding word. If a character already exists, I checked whether it points to the same word. If not, I returned false immediately. I also used containsValue() to make sure no word was reused for a different character. This problem reinforced the importance of careful logic flow, map lookups, and validation in both directions to avoid conflicts in mapping. 💡 Key learning: Building bijections with HashMaps is a common pattern in many string and mapping problems, and mastering it really improves problem-solving skills. #LeetCode #Day56 #Java #CodingChallenge #ProblemSolving #100DaysOfCode #DataStructures #HashMap #LearningEveryday
To view or add a comment, sign in
-
-
🔢 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
-
-
🌟 Day 63 of 100 Days of LeetCode 🌟 Today’s challenge: LeetCode 101 – Symmetric Tree 🔁 🧩 Problem Summary: Given the root of a binary tree, determine whether it’s a mirror of itself (i.e., symmetric around its center). 🌲✨ 💡 Key Takeaways: 🔹 Strengthened understanding of recursion in binary trees. 🔹 Learned how to compare mirror nodes (left ↔ right). 🔹 Realized that symmetry problems are perfect practice for recursive thinking and structural comparison. 🧠 Approach: 1️⃣ The tree is symmetric if its left and right subtrees are mirror images. 2️⃣ Use a helper function to compare: left.val == right.val left.left == right.right and left.right == right.left 3️⃣ Handle null checks carefully to avoid exceptions. 🔍 This problem beautifully shows how mirror logic + recursion can simplify complex tree checks! #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #ProblemSolving #CodingJourney #Recursion #SymmetricTree
To view or add a comment, sign in
-
-
💻 Day 53 of #LeetCode100DaysChallenge Solved LeetCode 160: Intersection of Two Linked Lists — a problem that tests linked list traversal, pointer manipulation, and logical thinking. 🧩 Problem: Given the heads of two singly linked lists headA and headB, return the node where the two lists intersect. If they do not intersect, return null. 💡 Approach — Two Pointer Technique: 1️⃣ Initialize two pointers a and b at headA and headB. 2️⃣ Traverse both lists. When a pointer reaches the end of one list, redirect it to the head of the other list. 3️⃣ If the lists intersect, the pointers will meet at the intersection node. 4️⃣ If not, both pointers will eventually become null. ⚙️ Complexity: Time: O(N + M) — each list is traversed at most twice. Space: O(1) — no extra data structure used. ✨ Key Takeaways: ✅ Strengthened understanding of pointer redirection and traversal synchronization. ✅ Learned an elegant approach to detect intersection without measuring lengths. ✅ Reinforced logical thinking in linked list-based problems. #LeetCode #100DaysOfCode #Java #LinkedList #Pointers #TwoPointerTechnique #DSA #CodingJourney #WomenInTech #IntersectionOfLinkedLists
To view or add a comment, sign in
-
-
🌟 Day 84 of My #100DaysOfCode Challenge 🧩 Problem: LeetCode 108 – Convert Sorted Array to Binary Search Tree 💭 Understanding the Problem Given a sorted array, we need to convert it into a height-balanced Binary Search Tree (BST) — meaning the difference in height between the left and right subtrees of every node should not exceed one. 📘 Example: Input: nums = [-10, -3, 0, 5, 9] Output: [0, -3, 9, -10, null, 5] The middle element (0) becomes the root, left half forms the left subtree, and the right half forms the right subtree. 🧠 Key Idea Pick the middle element as the root for balance. Recursively repeat the same for left and right halves. This approach ensures that the BST remains balanced. ⚙️ Complexity Analysis Time Complexity: O(n) — each element is processed once. Space Complexity: O(log n) — recursion stack in a balanced tree. ✨ Takeaway This problem beautifully combines recursion and binary tree logic, showcasing how dividing the array strategically helps maintain balance in a BST. 💬 "Balanced structures are key to efficiency — both in code and in life!" 😄 #Day84 #LeetCode #Java #DSA #BinaryTree #CodingChallenge #100DaysOfCode #ProblemSolving
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 15/100 - Majority Element (LeetCode 169) 🔹 Problem: Given an array of integers nums, find the element that appears more than ⌊ n/2 ⌋ times — the majority element. It’s guaranteed that such an element always exists in the array. 🔹 Approach: Implemented the Boyer–Moore Voting Algorithm, a clever and efficient approach: Assume the first element as the candidate. Traverse the array — increment votes if the element matches the candidate, otherwise decrement. If votes reach zero, update the candidate. The last candidate standing is the majority element. 🔹 Time Complexity: O(n) — only one traversal through the array. 🔹 Space Complexity: O(1) — uses constant extra space. 🔹 Key Learnings: Learned how voting logic simplifies counting-heavy problems. Achieved 99.76% faster runtime on LeetCode. Reinforced how simplicity and logic often outperform brute force. 💡 Every dataset has a voice — you just need the right logic to hear it. #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingChallenge #BoyerMooreAlgorithm
To view or add a comment, sign in
-
-
🚀 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
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