Day 12 of #50DaysOfLeetCode Challenge Today, I dived deep into one of the most fundamental yet tricky operations in a Binary Search Tree: Deleting a Node. At first glance, it seems simple, but the real challenge lies in re-connecting the nodes correctly so the BST property remains intact. Using a pen and paper to dry run the logic made a huge difference in understanding how the tree "heals" itself after a node is removed. The 3 Scenarios I explored: 1. The Leaf Node: Simple! Just remove it (set parent's child to null). 2. One Child: The node’s child simply "steps up" and takes its place. 3. Two Children (The Tricky Part): To keep the tree valid, we find the Inorder Successor (the smallest value in the right subtree), swap it with the target node, and then delete the successor. DSA isn't just about writing code; it’s about visualizing the structure and understanding the "why" behind every pointer change. #DataStructures #Algorithms #Java #LeetCode #CodingJourney #BST #ProblemSolving #ContinuousLearning
Deleting a Node in a Binary Search Tree
More Relevant Posts
-
🚀 Day 89 – DSA Journey | Invert Binary Tree Continuing my daily DSA practice, today I worked on a classic problem that builds strong intuition around tree manipulation. 📌 Problem Practiced: Invert Binary Tree (LeetCode 226) 🔍 Problem Idea: Given a binary tree, invert it by swapping the left and right children of every node. 💡 Key Insight: At each node, simply swap its left and right subtrees. Recursively applying this across the tree results in a complete mirror of the original tree. 📌 Approach Used: • If the node is null → return null • Swap left and right children • Recursively apply the same operation on both subtrees • Return the root after inversion 📌 Concepts Strengthened: • Tree traversal • Recursion • Tree manipulation • Mirror transformation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Simple operations applied recursively can transform complex structures like trees efficiently. On to Day 90! 🚀 #Day89 #DSAJourney #LeetCode #BinaryTree #Recursion #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 56 of My DSA Journey Today I solved LeetCode 162 – Find Peak Element on . 📌 Problem Given an array "nums", find a peak element and return its index. 👉 A peak element is one that is greater than its neighbors. You can assume: • "nums[-1] = -∞" and "nums[n] = -∞" --- 🧠 Approach – Binary Search (Optimized) Instead of checking every element (O(n)), I used Binary Search. Key Idea: • Calculate "mid" • Compare "nums[mid]" with "nums[mid + 1]" 👉 If "nums[mid] > nums[mid + 1]" → We are in a descending part, so peak lies on the left side (including mid) 👉 Else → We are in an ascending part, so peak lies on the right side Repeat until "start == end" → that index is a peak. --- ⏱ Time Complexity: "O(log n)" — Efficient binary search 📦 Space Complexity: "O(1)" — No extra space used --- 💡 Key Learnings ✔ Using binary search on unsorted-looking problems ✔ Understanding pattern-based decision making ✔ Learning that a problem can have multiple valid answers (any peak) This problem really strengthens intuition for advanced binary search patterns 🚀 --- Consistency continues — getting better every day 💪🔥 #100DaysOfCode #DSA #BinarySearch #LeetCode #Java #ProblemSolving #CodingJourney #DeveloperJourney #Programming #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 90 – DSA Journey | Binary Tree Paths Continuing my daily DSA practice, today I explored how to track and construct paths in a binary tree. 📌 Problem Practiced: Binary Tree Paths (LeetCode 257) 🔍 Problem Idea: Return all root-to-leaf paths in a binary tree as strings. 💡 Key Insight: While traversing the tree, we can build the path step by step. Once we reach a leaf node, we add the complete path to the result. 📌 Approach Used: • Use DFS traversal • Maintain a string to track the current path • At each node, append its value to the path • If it’s a leaf node → add the path to result • Continue exploring left and right subtrees 📌 Concepts Strengthened: • Tree traversal (DFS) • Recursion • Path tracking • String manipulation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Tracking state (like a path) during recursion is a powerful technique for solving tree problems. On to Day 91! 🚀 #Day90 #DSAJourney #LeetCode #BinaryTree #DFS #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Solved LeetCode Problem #69 – Sqrt(x) Today I worked on a classic problem that beautifully demonstrates how Binary Search can be applied beyond arrays. 🔍 Problem Insight: Given a non-negative integer x, the goal is to compute the integer square root (i.e., the floor value of √x) without using built-in functions. 💡 Approach Used: I applied Binary Search on the answer space: Defined a search range from 1 to x Checked whether mid * mid is less than, equal to, or greater than x Narrowed down the range accordingly Tracked the best possible answer when an exact square wasn’t found 🧠 Key Learning: Binary search isn’t just for searching—it can be used to optimize mathematical problems Handling integer overflow (using long) is crucial Understanding how to compute floor values in algorithmic problems 📈 Complexity: Time: O(log x) Space: O(1) ✨ This problem strengthened my understanding of: Binary Search on range/answer Efficient problem-solving techniques Writing optimized and scalable code Consistently solving such problems is helping me build strong fundamentals in DSA and problem-solving 💪 #LeetCode #DSA #BinarySearch #Algorithms #Coding #ProblemSolving #Java #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 87 – DSA Journey | Binary Tree Preorder Traversal Continuing my daily DSA practice, today I explored another important tree traversal technique using an iterative approach. 📌 Problem Practiced: Binary Tree Preorder Traversal (LeetCode 144) 🔍 Problem Idea: Traverse a binary tree in preorder — Root → Left → Right — and return the sequence of node values. 💡 Key Insight: Using a stack, we can simulate recursion and control the traversal order. By pushing the right child before the left, we ensure the left subtree is processed first. 📌 Approach Used: • Use a stack and start with the root node • Pop node, process (add to result) • Push right child first, then left child • Repeat until stack is empty 📌 Concepts Strengthened: • Tree traversal (Preorder) • Stack usage • Iterative vs recursive approach • Order control in traversal ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) 🔥 Today’s takeaway: Understanding traversal order and stack behavior makes it easier to convert recursive tree problems into iterative ones. On to Day 88! 🚀 #Day87 #DSAJourney #LeetCode #BinaryTree #Stack #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Day 86 – DSA Journey | Path Sum in Binary Tree Continuing my daily DSA practice, today I worked on a problem that strengthened my understanding of recursion and root-to-leaf traversal. 📌 Problem Practiced: Path Sum (LeetCode 112) 🔍 Problem Idea: Determine if there exists a root-to-leaf path in a binary tree such that the sum of node values equals a given target. 💡 Key Insight: Instead of tracking the full path, we can reduce the problem by subtracting the current node’s value from the target as we traverse down the tree. 📌 Approach Used: • If the node is null → return false • Check if it is a leaf node – If yes, compare node value with remaining target • Subtract current node value from target • Recursively check left and right subtrees • If any path matches → return true 📌 Concepts Strengthened: • Tree traversal • Recursion • Root-to-leaf path logic • Problem reduction technique ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Breaking down a problem step by step (reducing the target at each node) makes recursion much more intuitive. On to Day 87! 🚀 #Day86 #DSAJourney #LeetCode #BinaryTree #Recursion #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 Day 61 of my DSA Journey Today’s problem: Reverse Linked List 🔁 This is one of those classic problems that really tests your understanding of pointers and how data structures work internally. 💡 What I learned: How to reverse links instead of values Importance of using prev, curr, and next pointers How in-place algorithms help optimize space ⚡ Approach in short: Traverse the list once, and keep reversing the direction of each node step by step. 📊 Complexity: Time: O(n) Space: O(1) 🧠 Takeaway: DSA is not about memorizing solutions, it’s about understanding patterns. Every problem adds a new piece to the puzzle. Consistency > Motivation 💯 #DSAJourney #100DaysOfCode #Java #LeetCode #Coding #ProblemSolving #LinkedList #KeepLearning
To view or add a comment, sign in
-
-
🚀 DSA Practice — Getting Better Step by Step! 📅 Today’s Problem: Find Square Root of a Number (Floor Value) Solved using TakeUForward platform 💻 Today’s problem was a great example of applying Binary Search beyond just searching elements. 🔹 Approach Used: Binary Search on Answer Instead of checking every number, I used binary search to efficiently find the square root by narrowing down the range. ⏱ Time Complexity: O(log n) 💾 Space Complexity: O(1) 💡 Key Learning: Binary Search is not limited to sorted arrays — it can be applied to optimize problems where the answer lies within a range. This problem helped me understand how to reduce time complexity from O(n) to O(log n) by thinking in terms of search space rather than brute force. 🔥 Consistency continues — learning something new every day! takeUforward Striver #BinarySearch #takeUforwardtakeUforward #DSA #Algorithms #Coding #Java #ProblemSolving #SoftwareEngineering #Consistency
To view or add a comment, sign in
-
-
🚀 Day 84 – DSA Journey | Maximum Depth of Binary Tree Continuing my daily DSA practice, today I focused on understanding tree depth and recursive problem solving. 📌 Problem Practiced: Maximum Depth of Binary Tree (LeetCode 104) 🔍 Problem Idea: Find the maximum depth (or height) of a binary tree — the number of nodes along the longest path from the root to a leaf node. 💡 Key Insight: The depth of a tree depends on its subtrees. At every node, we can recursively calculate the depth of left and right subtrees and take the maximum. 📌 Approach Used: • If the node is null → depth is 0 • Recursively calculate depth of left subtree • Recursively calculate depth of right subtree • Return 1 + max(left, right) 📌 Concepts Strengthened: • Binary tree traversal • Recursion • Divide and conquer approach • Tree height calculation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) (recursion stack) 🔥 Today’s takeaway: Breaking problems into smaller subproblems using recursion makes complex tree problems much easier to handle. On to Day 85! 🚀 #Day84 #DSAJourney #LeetCode #BinaryTree #Recursion #Java #ProblemSolving #Coding #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 DSA Series #1 — Closest Target in Circular Array Today I solved an interesting problem involving circular arrays + shortest distance logic. 🧠 Key Insight: Instead of simulating movement, we can compute distance using modulo. 👉 Forward distance: (i - start + n) % n 👉 Backward distance: (start - i + n) % n Take the minimum of both — done in O(n) time ⚡ 📌 Complexity: O(n) time | O(1) space 💡 Learning: Circular problems often look tricky, but math simplifies everything. 🔥 Building consistency with: #DSA #Java #Coding #PlacementPreparation #100DaysOfCode If you’re on the same path, let’s connect and grow 🤝
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