🚀 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
Inverting a Binary Tree with Recursion
More Relevant Posts
-
🚀 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
-
-
🔢 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 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
-
-
✨ 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 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
-
-
✅ Just solved LeetCode #654 — Maximum Binary Tree 📘 Problem: Given an integer array without duplicates, the task is to build a maximum binary tree. The construction rules are: 1️⃣ The root is the maximum element in the array. 2️⃣ The left subtree is built recursively from elements to the left of the maximum. 3️⃣ The right subtree is built recursively from elements to the right of the maximum. Example: Input → [3,2,1,6,0,5] Output → [6,3,5,null,2,0,null,null,1] 🧠 My Approach: I solved this problem using a recursive divide-and-conquer approach. 1️⃣ Find the index of the maximum element in the given range — this becomes the root. 2️⃣ Recursively build the left subtree from the subarray before the maximum element. 3️⃣ Recursively build the right subtree from the subarray after the maximum element. 💡 What I Learned: ✅ How recursion naturally fits into tree construction problems ✅ The concept of divide and conquer applied to array-based tree building ✅ How to translate problem definitions into direct recursive structure #LeetCode #Java #DSA #BinaryTree #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
✨ Day 52 of 100: Copy List with Random Pointer ✨ Today’s challenge was LeetCode 138 – Copy List with Random Pointer 🧠 The task: Given a linked list where each node has two pointers — 🔹 next: points to the next node 🔹 random: points to any node in the list (or null) we need to create a deep copy of this list. 💡 Key Takeaways: 🔹 Learned how to handle complex data structures where nodes reference each other in non-linear ways. 🔹 Practiced creating a deep copy using HashMap to maintain the mapping between original and copied nodes. 🔹 Explored an optimized in-place approach — interleaving original and copied nodes to avoid extra space. 🔹 Strengthened understanding of pointer manipulation and object references in Java. 🚀 This problem deepened my appreciation for how data structure cloning works — not just duplicating values, but preserving relationships! #LeetCode #100DaysOfCode #Day52 #Java #LinkedList #DeepCopy #CodingJourney #ProblemSolving
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 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
-
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