Day 22/100 – DSA Journey Problem: Find Mode in Binary Search Tree (BST) Today’s problem focused on understanding how Binary Search Trees (BST) behave and how we can efficiently extract useful insights from them. Understanding the BST A Binary Search Tree follows a structured property: Left subtree → values ≤ root Right subtree → values ≥ root Because of this, when we perform an Inorder Traversal (Left → Root → Right), the values are visited in sorted order. Why Inorder Traversal? Since duplicates appear consecutively in a sorted sequence, inorder traversal allows us to: Track frequency without using extra space Compare current value with previous value Efficiently determine the most frequent element (mode) Approach Used Traverse the BST using inorder traversal Maintain: Previous value Current count Maximum frequency Update result list whenever a new maximum frequency is found Key Learning This problem highlights how leveraging tree properties can help optimize solutions. Instead of using extra space (like hashmaps), we used traversal behavior to achieve an efficient solution. Conclusion Understanding the underlying structure of data (like BST properties) is often more powerful than brute-force approaches. Smart traversal choices can significantly reduce space complexity and improve performance. #Day22 #100DaysOfCode #DSA #BinarySearchTree #Python #CodingJourney #LeetCode #ProblemSolving
Find Mode in Binary Search Tree using Inorder Traversal
More Relevant Posts
-
🚀 Day 22 of Consistency | #75DaysLeetCodeChallenge 🧠 Today’s Problem : Daily Temperatures (#739) 💡 Key Learning: This problem introduces the concept of a monotonic stack, useful for solving “next greater element” type problems efficiently. ⚡ Approach: Use a stack to store indices of temperatures Traverse the array → While current temp > stack top temp → pop & calculate days difference Store result for popped indices Push current index to stack 🧠 Why this works: Monotonic stack keeps decreasing order Helps find next greater element in O(n) Avoids brute force O(n²) 🔥 Result : ✔️ Runtime: 116 ms (Beats 28.07%) 📈 A very important pattern for stack-based optimization problems. A big thanks to Shivam Singh, Nikhil Yadav & Akshat Tiwari for this amazing challenge 🙌 Consistency is compounding. Keep going. 💪 #Day22 #LeetCode #DSA #CodingJourney #100DaysOfCode #Python #Stack #Consistency
To view or add a comment, sign in
-
-
🚀 Day 39/60 — LeetCode Discipline Problem Solved: Sqrt(x) Difficulty: Easy Today’s challenge was to compute the square root of a number without using built-in functions. Instead of brute force, I used Binary Search — a classic, elegant approach that narrows down the answer efficiently. 💡 Key Learnings: • Binary Search application beyond arrays • Handling edge cases (x < 2) • Avoiding overflow using conditions carefully • Finding floor value of square root • Optimized thinking over brute force ⚡ Performance: Runtime: 4 ms Like walking in a foggy path, I didn’t see the answer directly… But step by step— cutting the search space in half— the truth revealed itself. That’s the beauty of algorithms. #LeetCode #60DaysOfCode #DSA #BinarySearch #ProblemSolving #CodingJourney #Python #Consistency #TechGrowth
To view or add a comment, sign in
-
-
From Linear Regression to Logistic Regression. After building Linear Regression from scratch, I wanted to take the next step — solving a classification problem. I implemented Logistic Regression the same way (no libraries), to classify whether an email is Spam or Not Spam. - What I did: Implemented the sigmoid function to convert outputs into probabilities Built the prediction logic for binary classification Used gradient descent to optimize parameters (w, b) Applied a threshold (0.5) to classify outputs - What I learned: Difference between regression (continuous output) and classification (probability-based output) How sigmoid creates an S-shaped curve for decision making Why loss functions like log loss are used in classification - Result: The model learns to classify based on input activity and outputs probabilities. Example: Activity: 3.0 → Probability: ~0.90 → Spam 🔗 Code: https://lnkd.in/g_MygMKc #MachineLearning #Python #LogisticRegression #LearningInPublic
To view or add a comment, sign in
-
-
Day 14/100 – Data Structures & Algorithms Today, I worked on the problem “First Unique Character in a String.” Overview The task is to identify the first non-repeating character in a string and return its index. If no such character exists, the result is -1. Approach I used a two-pass strategy: • First pass to store character frequencies using a hashmap • Second pass to identify the first character with a frequency of one Complexity • Time Complexity: O(n) • Space Complexity: O(1) Key Takeaway This problem reinforces how effective hashmaps are for frequency-based problems and how a simple two-pass approach can lead to optimal solutions. Staying consistent and building problem-solving intuition step by step. #Day14 #100DaysOfCode #DSA #Python #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 118 Backtracking patterns are repeating — and getting clearer. #Day118 🧩 39. Combination Sum How today went: • Used index i to control choices • Two options at each step: → stay at i (reuse same element) → move to i + 1 (try next element) • Track current total • If total == target → store result • If total > target → backtrack What clicked: It’s all about: → controlling index → tracking total → backtracking at the right time Same pattern, more confidence. #LeetCode #DSA #Python #Backtracking #Recursion #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Day 20 of My DSA Journey – Daily Temperatures Today’s problem: Daily Temperatures 🌡️ A great example of how switching from brute force to the right data structure changes everything. 🔍 Problem Given a list of temperatures, find how many days you need to wait for a warmer day. If no warmer day exists, return 0. 💡 What I Learned At first glance, a nested loop feels natural… but that leads to O(n²) time complexity ❌ The optimized solution uses a Monotonic Stack to reduce it to O(n) ✅ ⚡ Approach Use a stack to store indices of unresolved temperatures Traverse the array once When a warmer temperature appears → resolve previous days Store the difference in indices #Day20 #DSA #LeetCode #Python #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Built a Rainfall Prediction model and deployed it live. Here is what actually happened behind the scenes Decision Tree gave me 100% training accuracy. I got excited. Then I checked the test score and realised the model had just memorised the data. It learned nothing real. Naive Bayes gave me 73.9% on both train and test. Consistent That is the one I deployed. 3 models trained. 1 deployed. 1 lesson — a consistent score beats a perfect score every time live app here: https://lnkd.in/d-xaufug Full project and code: https://lnkd.in/d_d2Tx7R Akarsh Vyas Tanishq Vyas #DataScience #MachineLearning #Python #Streamlit #BuildInPublic #MLProject
To view or add a comment, sign in
-
🚀 Day 28 of Problem Solving Journey Today, I worked on an interesting problem: Group Anagrams 🔍 Problem Statement: Given an array of strings, group the anagrams together. Anagrams are words that have the same characters but arranged differently. 💡 Approaches I explored: ✅ Approach 1: Character Frequency Count (Optimized) Used a fixed-size array (26 letters) to count character occurrences Converted the count into a tuple to use as a dictionary key Achieved an efficient time complexity of O(n * k) ✅ Approach 2: Sorting Strings Sorted each string and used it as a key Simple and intuitive approach Time complexity: O(n * k log k) 📌 Key Learning: Understanding how hashing works with different representations (frequency vs sorted string) helps in optimizing solutions. ⚡ Takeaway: There are always multiple ways to solve a problem, but choosing the most efficient one makes a difference in real-world applications and interviews. 💻 Tech Used: Python | HashMap | Arrays #Day28 #ProblemSolving #Python #DataStructures #Algorithms #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Days 68-69 of the #three90challenge 📊 Today I explored NumPy operations — specifically indexing and slicing arrays. After understanding NumPy basics, this step made it easier to access and manipulate data efficiently. What I practiced today: • Accessing elements using indexing • Extracting subsets of data using slicing • Working with multi-dimensional arrays • Performing operations on selected data Example thinking: Instead of looping through data manually, I can directly select and operate on specific parts of an array. Example: import numpy as np arr = np.array([10, 20, 30, 40, 50]) print(arr[1:4]) # Output: [20 30 40] This makes data manipulation faster and more intuitive. From handling data → to controlling it efficiently 🚀 GeeksforGeeks #three90challenge #commitwithgfg #Python #NumPy #DataAnalytics #LearningInPublic #Consistency #Upskilling
To view or add a comment, sign in
-
LeetCode Problem 1448. Count Good Nodes in Binary Tree: "Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. Return the number of good nodes in the binary tree." Approach: make use of pre-order traversal i.e., visit root->left->right child, in each visit, keep track of the max element upto that point by maintaining a stack, top of the stk should represent the max element upto that point in the path, pop() elements from stack when both the children (left,right) are explored. Time Complexity: O(n) Space Complexity: O(n) b/c of maintaining a stack #Python #LeetCode #DSA #DataStructures #Algorithms #Stack #Recursion #DFS #BinaryTree #ProblemSolving
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