🌳 Day 62 of 100 Days of LeetCode 🌳 Today’s challenge: LeetCode 226 – Invert Binary Tree 🔄 🧩 Problem Summary: Given the root of a binary tree, invert it — meaning swap the left and right children of every node. Essentially, you’re mirroring the entire tree! 🌲✨ 💡 Key Takeaways: 🔹 Strengthened understanding of tree traversal using recursion. 🔹 Learned how a simple DFS (Depth-First Search) can elegantly solve structural transformations. 🔹 Practiced reasoning about symmetry and recursion base cases. 🧠 Approach: 1️⃣ If the tree is empty, return null. 2️⃣ Recursively invert the left and right subtrees. 3️⃣ Swap the left and right children of the current node. 4️⃣ Return the root once the tree is fully inverted. 🚀 A great example of how recursion simplifies complex problems into smaller, intuitive steps! #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #CodingJourney #Recursion #ProblemSolving
Inverting a Binary Tree with Recursion on LeetCode
More Relevant Posts
-
🌟 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 54 of My #100DaysOfLeetCode Challenge 🚀 Today I worked on a problem related to making parentheses valid — a classic stack-based problem that tests your understanding of balanced brackets and edge cases. 💡 Problem: Given a string containing only '(' and ')', determine the minimum number of parentheses that must be added to make the string valid. 🧠 Concepts Used: Stack data structure String traversal Counting unmatched parentheses 🧩 Approach: Traverse through the string character by character. Use a stack to track unmatched '('. When encountering ')', pop from the stack if possible, else increment the counter. Finally, the total unmatched parentheses = stack size + counter. #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney #DSA #WomenInTech #TechLearning #CodeNewbie
To view or add a comment, sign in
-
-
🚀 LeetCode 150 – Lowest Common Ancestor of a Binary Tree 🌳 A recursion-based problem from the LeetCode 150 list! The goal: find the lowest common ancestor (LCA) of two given nodes in a binary tree. 🔹 Approach If the current node is null, return null. If the node matches p or q, return it. Recursively check left and right subtrees. If both return non-null → current node is the LCA. Otherwise, propagate the non-null result upward. 💡 Time: O(N) | Space: O(H) #LeetCode150 #DSA #Java #Recursion #BinaryTree #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 43 of #100DaysOfLeetCode Today's problem: LeetCode #160 – Intersection of Two Linked Lists 💡 Concept: Find the node where two singly linked lists intersect. Used the Two Pointer Approach — a smart and efficient way to solve this without using extra memory. 🧠 Logic: Move both pointers through the lists. When one pointer reaches the end, switch it to the other list’s head. They’ll either meet at the intersection node or end up as null together. ✅ Complexity: Time – O(n + m) Space – O(1) 💬 Takeaway: Sometimes, the best solutions come from balancing the path — literally! Understanding how pointers sync up teaches a lot about memory references and linked list behavior. #LeetCode #CodingChallenge #Java #DataStructures #TwoPointerTechnique #ProblemSolving #LinkedLists
To view or add a comment, sign in
-
-
🌳 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
-
-
💻 LeetCode 50 Days Challenge — Day 3: Remove Duplicates from Sorted Array Day 3 of my #LeetCode50DaysChallenge ✅ Today’s problem was about array manipulation — Remove Duplicates from Sorted Array ✨ 🧩 Problem: Given an integer array nums sorted in non-decreasing order, remove duplicates in-place such that each unique element appears only once. The relative order of the elements should remain the same. 💡 Approach: This problem is a classic two-pointer approach! One pointer i keeps track of the last unique element’s position. The other pointer j iterates through the array. Whenever a new element is found (nums[i] != nums[j]), we move it forward by incrementing i and assigning nums[i] = nums[j]. In the end, i + 1 gives the count of unique elements. A simple yet elegant technique to modify arrays in-place! ⏱️ Time Complexity: O(n) 📊 Example: Input: [0,0,1,1,1,2,2,3,3,4] Output: [0,1,2,3,4] Consistency is the secret ingredient to progress! 🌱 Each problem solved adds another brick to the wall of mastery 💪 #LeetCode #CodingChallenge #Day3 #ProblemSolving #Java #SoftwareDevelopment #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🧠 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
To view or add a comment, sign in
-
-
🚀 Day 8 of #45DaysOfLeetCode Challenge 😎 📌Today's problem: Palindrome Number (LeetCode #9) 💡 🔹 Concept: Check whether a given integer reads the same backward and forward — without converting it to a string! 🔹 Logic Used: Instead of reversing the entire number, I reversed only half of it to improve efficiency. This avoids unnecessary computation and eliminates integer overflow risks. 🔹 Key Learnings: ✅ Optimized logic using mathematical manipulation ✅ Improved understanding of number reversal techniques ✅ Importance of edge case handling (negative numbers, trailing zeros) ⚙️ Result: 💻 Runtime: 4 ms (Beats 100%) 💾 Memory: 44.84 MB 😎Small optimizations make a big difference in performance! 💪 📌Problem: https://lnkd.in/ef6AC2j6 🔥Each day is a step closer to writing cleaner and more optimized code. ✨ Let’s keep pushing forward and refining our problem-solving skills! 💻🔥 #LeetCode #Day8 #100DaysOfCode #ProblemSolving #Java #CodingChallenge #PalindromeNumber #DataStructures #Algorithms #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 47 of My LeetCode Journey 🚀 Problem: Sum of Square Numbers Topic: Math / Two Pointers 🧠 Approach: To check if a number c can be expressed as the sum of squares of two integers (a² + b² = c), we use the two-pointer technique: Start a = 0 and b = √c Calculate sum = a² + b² If sum == c → ✅ return true If sum < c → increase a If sum > c → decrease b Repeat until a <= b. ⏱️ Complexity: Time: O(√c) Space: O(1) This problem beautifully combines mathematical logic with an efficient two-pointer approach — clean and elegant! 💡 #100DaysOfCode #LeetCode #Java #CodingChallenge #ProblemSolving #DSA
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