Day 77/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Binary Search Tree to Greater Sum Tree A clean and elegant BST transformation problem. Problem idea: Convert a BST so that each node’s value becomes the sum of all values greater than or equal to it. Key idea: Reverse inorder traversal (Right → Root → Left). Why? • In a BST, inorder gives sorted order • Reverse inorder processes nodes from largest to smallest • Maintain a running sum while traversing How it works: • Start from the rightmost node (largest value) • Keep a cumulative sum variable • Add current node value to sum • Update node value with sum • Move to left subtree Time Complexity: O(n) Space Complexity: O(h) (stack / recursion depth) Big takeaway: Whenever you need greater values accumulation in BST, think of reverse inorder traversal. 🔥 This pattern is very useful in BST transformation problems. Day 77 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearchTree #Trees #Recursion #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
Yogesh ..’s Post
More Relevant Posts
-
Day 79/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Flatten Binary Tree to Linked List A powerful tree transformation problem using pointer manipulation. Problem idea: Convert a binary tree into a linked list in-place following preorder traversal. Key idea: Iterative traversal + rewiring (similar to Morris traversal idea). Why? • We need preorder sequence (Root → Left → Right) • Instead of extra space, we modify pointers in-place • Efficient and avoids recursion stack How it works: • Traverse using a pointer curr • If left child exists: → Find rightmost node of left subtree → Connect it to current’s right subtree → Move left subtree to right → Set left = null • Move to next node (curr.right) Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Tree problems can often be optimized using in-place pointer rewiring, avoiding extra space. 🔥 This pattern is very useful for tree flattening and traversal optimizations. Day 79 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #MorrisTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 82/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Lowest Common Ancestor of a Binary Search Tree A clean problem that shows the power of BST properties. Problem idea: Find the lowest common ancestor (LCA) of two nodes in a BST. Key idea: Use BST ordering to decide direction (no need to explore entire tree) Why? • In a BST: left < root < right • If both nodes are smaller → go left • If both nodes are greater → go right • Otherwise → current node is the LCA How it works: • Start from root • If p and q are both < root → move left • If p and q are both > root → move right • Else → you’ve found the split point (LCA) Time Complexity: O(h) Space Complexity: O(1) (iterative approach) Big takeaway: Whenever working with BST, always use its ordering property to optimize traversal. 🔥 This avoids unnecessary recursion and makes the solution super efficient. Day 82 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearchTree #TreeTraversal #LCA #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🧠 Day 206 — Minimum Distance to Target 🎯📍 Today solved a simple yet important problem and did some revision alongside. 📌 Problem Goal Given an array, a target value, and a starting index: ✔️ Find the minimum distance between the start index and any index where the target exists 🔹 Core Idea Traverse the array and: ✔️ Check if current element matches the target ✔️ Calculate distance from the start index ✔️ Keep updating the minimum distance 🔹 Key Observation ✔️ Only indices with the target matter ✔️ Distance = absolute difference between indices ✔️ Simple linear scan gives optimal solution 🧠 Key Learning ✔️ Even easy problems strengthen fundamentals ✔️ Brute-force with clarity > overthinking ✔️ Absolute difference patterns are very common in arrays 💡 Today’s Realization Not every day needs to be a hard problem. Consistency with simple + revision days builds stronger intuition. 🚀 Momentum Status: Staying consistent and sharpening basics. On to Day 207. #DSA #Arrays #ProblemSolving #LeetCode #Java #CodingJourney #ConsistencyWins
To view or add a comment, sign in
-
Day 69/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Reverse Nodes in k-Group A challenging linked list problem focused on reversing nodes in fixed-size groups. Problem idea: Reverse nodes of a linked list in groups of size k, while keeping remaining nodes as it is. Key idea: Linked list manipulation + iterative reversal in segments. Why? • We need to reverse nodes in chunks, not the whole list • Careful pointer handling is required • Must preserve connections between groups How it works: • Use a dummy node for easier handling of head • Identify the k-th node from current position • Reverse nodes between current and k-th node • Connect reversed group back to the list • Move to next group and repeat • Stop if remaining nodes are less than k Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Mastering pointer manipulation in linked lists is key to solving advanced problems efficiently. This problem is a great example of combining reversal logic with structural control. 🔥 Day 69 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #LinkedList #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 65/100 | #100DaysOfDSA 🧩🚀 Today’s problem: N-Queens A classic hard backtracking problem. Problem idea: Place N queens on an N×N chessboard such that no two queens attack each other. Key idea: Use backtracking with efficient conflict checking. Why? • We must explore all valid board configurations • Reject placements that cause conflicts • Continue building only valid states How it works: • Place one queen per row • Track columns, diagonals, anti-diagonals • Use bit masking for faster checks ⚡ • Try placing → recurse → backtrack Optimization: Using bitmasks drastically reduces time compared to naive checking. Time Complexity: Exponential (but optimized with pruning) Space Complexity: O(n) recursion depth Big takeaway: Efficient state tracking (cols + diagonals) turns a brute-force problem into a much faster solution. 🔥 One of the most important backtracking problems to master! Day 65 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #Backtracking #Recursion #BitManipulation #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 84/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Recover Binary Search Tree A very interesting problem that tests your understanding of BST inorder properties. Problem idea: Two nodes in a BST are swapped by mistake. Recover the tree without changing its structure. Key idea: Inorder traversal of a BST should be sorted Why? • BST inorder traversal gives increasing order • If two nodes are swapped → order gets violated • Detect these violations to identify the wrong nodes How it works: • Perform inorder traversal • Track previous node (prev) • If prev.val > curr.val → violation found • First violation → mark first = prev • Second violation → mark second = curr • After traversal → swap values of first and second Optimization (used in your code): Use Morris Traversal to achieve O(1) space (no recursion/stack) Time Complexity: O(n) Space Complexity: O(1) (Morris Traversal) Big takeaway: Whenever working with BST, inorder traversal is your strongest tool. 🔥 Also, detecting anomalies in sorted order is a powerful technique. Day 84 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearchTree #MorrisTraversal #InorderTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
#Day362 of #1001DaysOfCode 📘 LeetCode Daily Challenge Problem: XOR After Queries 💡 Approach: Optimized the brute-force simulation using sqrt decomposition. Large step queries were processed directly Small step queries were grouped and batch-processed efficiently Used modular exponentiation + grouped progression updates to reduce time complexity significantly. ⏱ Optimized Time Complexity: ~O(q√n + n√n log MOD) 🧠 Space Complexity: O(n + q) A great problem for learning advanced optimization techniques 🚀 #DSA #Java #LeetCode #ProblemSolving #Algorithms #Coding
To view or add a comment, sign in
-
-
Day 68/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Add Two Numbers A fundamental linked list problem that mimics real-life addition. Problem idea: Add two numbers represented by linked lists (digits stored in reverse order). Key idea: Linked list traversal + carry handling. Why? • We process digits one by one (like manual addition) • Need to handle carry at each step • Lists can have different lengths How it works: • Use a dummy node to build the result • Traverse both lists simultaneously • Add values + carry • Create new node with (sum % 10) • Update carry = sum / 10 • Move pointers forward • Continue until both lists and carry are done Time Complexity: O(max(m, n)) Space Complexity: O(max(m, n)) Big takeaway: Using a dummy node simplifies linked list construction and avoids edge cases. This pattern is very common in linked list problems. 🔥 Day 68 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #LinkedList #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 83/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Validate Binary Search Tree A core problem that tests your understanding of BST rules beyond just parent-child relationships. Problem idea: Check whether a given binary tree is a valid BST. Key idea: Maintain a valid range (min, max) for every node Why? • BST rule applies to the entire subtree, not just immediate children • Left subtree must be strictly less than root • Right subtree must be strictly greater than root • Each node must lie within its allowed range How it works: • Start with range (-∞, +∞) • For each node: • Check if value lies within (min, max) • Recursively validate left subtree with updated max = node.val • Recursively validate right subtree with updated min = node.val Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Big takeaway: Never validate BST using only local checks — always think in terms of global constraints (ranges). 🔥 This pattern is extremely common in tree validation problems. Day 83 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearchTree #Recursion #TreeTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
💡 LeetCode 191 — Number of 1 Bits (Hamming Weight) Recently solved an interesting bit manipulation problem that highlights the power of low-level optimization 🚀 🔍 Problem Statement: Given an unsigned integer, count the number of set bits (1s) in its binary representation. 🧠 Key Insight: Instead of checking every bit individually, we can use a clever trick: 👉 n & (n - 1) removes the rightmost set bit in each operation. This allows us to count only the set bits, making the solution more efficient. ⚙️ Approach Used (Brian Kernighan’s Algorithm): Initialize a counter Repeatedly apply n = n & (n - 1) Increment count until n becomes 0 📈 Time Complexity: O(k), where k = number of set bits (faster than checking all 32 bits) 📦 Space Complexity: O(1) ✨ Why this problem is important: Strengthens understanding of bit manipulation Frequently asked in interviews Useful in low-level optimization and system design 💬 Takeaway: Sometimes the best solutions come from understanding how data is represented at the binary level. Small tricks can lead to big optimizations! #LeetCode #DSA #CodingInterview #Java #BitManipulation #ProblemSolving #TechJourney
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