Day 80/100 – #100DaysOfCode 🚀 | #Java #BinarySearchTree #DFS ✅ Problem Solved: Find Mode in Binary Search Tree (LeetCode) 🧩 Problem Summary Given the root of a Binary Search Tree, return all the mode(s) — the value(s) that appear most frequently in the tree. 📌 Note: BST property → inorder traversal gives sorted order There may be multiple modes 💡 Approach Used ✔ Use inorder traversal (DFS) to process values in sorted order ✔ Track: prev → previous node value count → current frequency maxCount → highest frequency seen so far ✔ Logic: If current value == prev → increment count Else → reset count to 1 Update result list when: count > maxCount → clear list & add value count == maxCount → add value This avoids extra space like HashMaps by leveraging BST properties. ⚙ Time Complexity: O(N) 📦 Space Complexity: O(H) (recursion stack) ✨ Takeaway Understanding tree properties (like sorted inorder traversal in BST) can eliminate unnecessary data structures and lead to cleaner solutions. #Java #LeetCode #BST #DFS #TreeTraversal #100DaysOfCode #CodingChallenge
Java Binary Search Tree Mode Finder LeetCode Challenge
More Relevant Posts
-
Day 91/100 – #100DaysOfCode 🚀 | #Java #BinaryTree #BFS ✅ Problem Solved: Find Largest Value in Each Tree Row (LeetCode) 🧩 Problem Summary: Given the root of a binary tree, return an array containing the largest value in each level (row) of the tree. 💡 Approach Used: ✔ Used Breadth-First Search (Level Order Traversal) ✔ Process the tree level by level using a queue For each level: Initialize maxValue to the smallest possible integer Traverse all nodes at that level Update maxValue Add it to the result list ⚙ Time Complexity: O(N) 📦 Space Complexity: O(W) (where W = maximum width of the tree) ✨ Takeaway: Level-order traversal is ideal when problems require row-wise processing in trees — clean, intuitive, and efficient. #Java #LeetCode #BinaryTree #BFS #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 89/100 – #100DaysOfCode 🚀 | #Java #BinaryTree #BFS ✅ Problem Solved: Find Bottom Left Tree Value (LeetCode 513) 🧩 Problem Summary: Given the root of a binary tree, return the leftmost value in the last row of the tree. 💡 Approach Used: ✔ Used Level Order Traversal (BFS) ✔ Traverse the tree level by level ✔ For each level, store the first (leftmost) node value ✔ The value from the last level is the answer This guarantees we always capture the correct bottom-left value. ⚙ Time Complexity: O(N) 📦 Space Complexity: O(N) (queue for BFS) ✨ Takeaway: Level-order traversal is ideal when problems involve row-wise processing or depth-based values in trees. #Java #LeetCode #BinaryTree #BFS #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 86/100 – #100DaysOfCode 🚀 | #Java #Tree #DFS ✅ Problem Solved: Most Frequent Subtree Sum (LeetCode 508) 🧩 Problem Summary Given the root of a binary tree, compute the sum of every subtree and return the value(s) that occur most frequently. A subtree sum is defined as the sum of all node values in that subtree, including the node itself. 💡 Approach Used ✔ Use postorder DFS to compute subtree sums ✔ For each node: SubtreeSum = leftSum + rightSum + node.val ✔ Store frequency of each sum using a hashmap ✔ Track the maximum frequency ✔ Collect all sums whose frequency equals the maximum ⚙ Time Complexity: O(N) 📦 Space Complexity: O(N) ✨ Takeaway Postorder traversal is ideal when child computations are required before processing the parent — a classic and powerful tree pattern. #Java #LeetCode #BinaryTree #DFS #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 3 / 100 – #100DaysOfCode Today’s focus was on Java backend fundamentals and deep DSA understanding. ⸻ 🔧 Development (Java) 📌 Local DB using JSON • Used JSON as a lightweight local database • Applied Java Serialization to convert objects ↔ JSON • Linked serialized JSON data with Java classes for persistent storage • Helpful for small-scale apps where a full DB is overkill 🧠 DSA Progress 1️⃣ Set Matrix Zeroes Problem: If an element is 0, set its entire row and column to 0 Optimized Approach (O(1) Space): • Use first row & first column as markers • If matrix[i][j] == 0: • Mark matrix[i][0] = 0 • Mark matrix[0][j] = 0 • Maintain two flags: • rowZero → whether first row contains 0 • colZero → whether first column contains 0 • Traverse matrix (excluding first row/column) and update based on markers • Finally, update first row & column using flags 2️⃣ Word Search Problem: Check if a word exists in a 2D board Approach: • Start DFS from cells matching first character • Move in 4 directions (up, down, left, right) • Use backtracking: • Mark cell as visited • Revert after recursive call • Stop early if index exceeds word length or characters mismatch. 3️⃣ Find the Duplicate Number Problem: Find the single duplicate in an array of size n+1 Efficient Approach (Index Marking): • For each element x: • Go to index abs(x) • If value at that index is already negative → duplicate found • Else, mark it negative My solution links- https://lnkd.in/gf4b-E6K https://lnkd.in/gNv9gtgB https://lnkd.in/gGatDNSK 📌 Consistency > Motivation. Building logic daily, one problem at a time. #100DaysOfCode #Day3 #Java #DSA #ProblemSolving #LeetCode #BackendDevelopment #LearningInPublic #SoftwareEngineer #Consistency #PlacementPrep
To view or add a comment, sign in
-
-
Day 57/100 of #100DaysOfCode 🚀 “Count Good Nodes in a Binary Tree” problem on LeetCode.. A node is considered good if there is no node with a greater value on the path from the root to that node. Approach: ->Traversed the tree using recursion ->Passed the maximum value seen so far along the root-to-node path ->Compared the current node’s value with the max: ->If node.val >= max, it’s a good node ->Updated the max value before going deeper ->Recursively counted good nodes in left and right subtrees ->Summed up the counts to get the final result #Day57 #100DaysOfCode #LeetCode #BinaryTree #DFS #Recursion #Java #DSA #ProblemSolving #LearningEveryDay
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 105 Backtracking & Recursion 🔹 Problem:- Combination Sum (LeetCode 39) Task: Given an array of distinct integers and a target value, find all unique combinations where the chosen numbers sum to the target. Each number can be used multiple times. Example: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3], [7]] My Approach: Used Backtracking to explore all possible combinations. At each step, decided to pick or skip the current element. Allowed reuse of elements by staying on the same index. Backtracked once the target became negative or a valid combination was found. Time Complexity: Exponential (based on number of combinations) Space Complexity: O(Target) (recursion stack + current path) Key Takeaway: Backtracking is all about choices and reversals. Once the pick / not-pick pattern becomes clear, complex problems start feeling much simpler. #takeUforward #200DaysOfCode #Java #Backtracking #Recursion #ProblemSolving #LeetCode #DSA #CodeNewbie #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 107 Subsets with Duplicates Problem:- Subsets II (LeetCode 90) Task: Given an integer array nums that may contain duplicates, return all possible unique subsets (power set). Example: Input: nums = [1,2,2] Output: [[], [1], [1,2], [1,2,2], [2], [2,2]] My Approach: Sorted the array to bring duplicate elements together. Used Backtracking to generate all subsets. Skipped duplicate elements at the same recursion level to avoid repeating subsets. Complexity Analysis: Time Complexity: O(2^N) Space Complexity: O(N) (recursion stack, excluding output) Handling duplicates becomes much easier when the input is sorted and recursion levels are controlled carefully. Backtracking problems look complex at first but the logic becomes clean once broken into steps. #takeUforward #100DaysOfCode #Java #LeetCode #ProblemSolving #Backtracking #Recursion #DSA #CodingJourney #CleanCode #DeveloperLife
To view or add a comment, sign in
-
-
Day 95/100 – #100DaysOfCode 🚀 | #Java #HashMap #Randomization ✅ Problem Solved: Random Flip Matrix (LeetCode 519) 🧩 Problem Summary: You are given a binary matrix initialized with all 0s. Each call to flip() should randomly return the position of a 0 and change it to 1. No position should be flipped more than once until reset() is called. 💡 Approach Used: ✔ Used HashMap + Randomization to simulate shuffling ✔ Treat the matrix as a flattened 1D array Steps: Map each index to its actual value using a HashMap Randomly pick an index from the remaining unflipped range Swap it with the last available index Decrease the available range On reset(), clear the map and restore the range ⚙ Time Complexity: O(1) per flip() 📦 Space Complexity: O(K) (where K = number of flipped cells) ✨ Takeaway: This problem is a great example of achieving uniform randomness without storing the entire matrix — smart indexing beats brute force. #Java #LeetCode #HashMap #Randomization #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day - 59 Subsets The problem - Given array of unique integers, return all possible subsets (power set). Solution must not contain duplicate subsets. Example : nums = [1,2,3] → [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Brute Force - Generate all combinations iteratively, still exponential, but less elegant than recursion. Approach Used - •) Call createSubset(nums, 0, res, subset). •) Base case, if index == nums.length, add current subset to result. •) Recursive First case, include nums[index], add to subset, recurse with index+1, backtrack (remove). •) Recursive Second case, exclude nums[index]: recurse with index+1 (don't add). Complexity - Time - O(n × 2^n),each element has 2 choices. Space - O(n), recursion depth. Note - For each element, make two choices, include it or exclude it. Recursively build all combinations #DSA #Java #SoftwareEngineering #InterviewPrep #LearnToCode #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
-
Good APIs don't just work - they fail gracefully. 💎 I recently implemented this structured exception builder to move away from messy, scattered error logic. By centralizing how we build validation exceptions, we ensure that every failure returns a rich, actionable payload. Notice the JSON output: - Timestamp for log correlation. - Documentation URLs for self-service fixing. - Request IDs for instant tracing. As we know, while other JVM languages offer great syntax, the power of Java lies in building these highly observable, scalable enterprise patterns. Quality isn’t just about the "happy path." It’s about what happens when things go wrong. How do you handle errors in your projects? Let's discuss in the comments! 👇 #Backend #API #Microservices #JavaDeveloper #CodingStandard
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