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
Java Binary Tree Largest Value in Each Row
More Relevant Posts
-
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 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
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
-
-
Day 97/100 – #100DaysOfCode 🚀 | #Java #Strings #Logic ✅ Problem Solved: Longest Uncommon Subsequence I (LeetCode) 🧩 Problem Summary: Given two strings, find the length of the longest uncommon subsequence — a subsequence that appears in one string but not in the other. 💡 Approach Used: ✔ Observational / logical approach ✔ If both strings are equal, no uncommon subsequence exists ✔ If they are different, the longer string itself is the answer Why it works: A string is always a subsequence of itself If strings differ, the longer one cannot be a subsequence of the shorter ⚙ Time Complexity: O(1) 📦 Space Complexity: O(1) ✨ Takeaway: Some problems look like DP but are solved with pure logic. Always check for simple observations before overengineering. #Java #LeetCode #Strings #Logic #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 108 Combination Sum III Problem:- Combination Sum III Task:- Find all valid combinations of k numbers that sum up to n, using only numbers from 1 to 9, where: Each number is used at most once No duplicate combinations are allowed Example: Input: k = 3, n = 7 Output: [[1, 2, 4]] My Approach:- Used Backtracking (DFS) to explore all possible combinations. Started from a given number to avoid duplicates. Stopped recursion when: The combination size exceeded k The sum became negative Added the combination to the result only when: Exactly k numbers were chosen The sum became 0 Time Complexity:- Exponential (bounded due to range 1–9) Space Complexity:- O(k) (recursive stack + temporary list) Backtracking may look complex at first, but with clear base conditions and pruning, it becomes a powerful and elegant tool for solving combination problems efficiently. #takeUforward #200DaysOfCode #Java #ProblemSolving #LeetCode #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie
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 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
-
-
I just published a quick 5-minute read on Bloom Filters in Java, breaking down where they fit, how they work, and why “probably” can sometimes be better than “definitely” in large-scale systems. It’s meant to be an easy, practical read, perfect for a Friday lunchtime scroll if you’re curious about how real systems avoid unnecessary database lookups and scale efficiently. Hope it’s useful, and would love to hear your thoughts. #Java #BloomFilter #BackendEngineering #SystemDesign #DataStructures #ScalableSystems
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
-
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