🚀 LeetCode Milestone: Problem #1448 – Count Good Nodes in Binary Tree (Medium) Today I solved another interesting binary tree problem that sharpened my DFS (Depth-First Search) skills. 🔹 Problem Statement: A node in a binary tree is considered good if, along the path from the root to that node, there are no values greater than the node itself. The task is to count all such good nodes. 🔹 Key Insight: Use DFS traversal. Track the maximum value seen so far along the path. If the current node’s value is greater than or equal to this maximum, it’s a good node. Update the maximum as we go deeper. 🔹 Example: Input: [3,1,4,3,null,1,5] Output: 4 Explanation: Nodes (3, 4, 5, 3) are good. 🔹 Learning: This problem reinforced the importance of carrying state (like max-so-far) during recursion. It’s a neat example of how DFS can elegantly solve path-dependent conditions in trees. 💡 Every solved problem adds confidence and clarity to my coding journey. Looking forward to tackling more challenging problems and sharing my progress! #LeetCode #CodingInterview #Java #DSA #BinaryTree #ProblemSolving #BackendDeveloperJourney
Count Good Nodes in Binary Tree with DFS
More Relevant Posts
-
Day 57 - LeetCode Journey Solved LeetCode 2104: Sum of Subarray Ranges (Medium) today using a brute force + optimization approach. This journey isn’t about solving the hardest problems daily, but about showing up consistently and improving step by step. Problems like this help in building a deeper understanding of arrays and how subarrays behave. Today’s problem reinforced key concepts like: • Understanding how to generate all subarrays efficiently • Tracking minimum and maximum values dynamically • Avoiding recomputation using smart updates • Writing clean and logical nested loop solutions The idea is simple — for each starting index, expand the subarray and keep updating min and max to calculate the range 💡 Every problem adds a new layer to problem-solving skills, and that’s where real growth happens 💯 ✅ Better understanding of subarrays and ranges ✅ Improved thinking for optimization ✅ More confidence in array-based problems Still a long way to go, but progress is progress 🚀 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #Algorithms #Programming #DeveloperJourney #KeepCoding
To view or add a comment, sign in
-
-
Day 53 - LeetCode Journey Solved LeetCode 80: Remove Duplicates from Sorted Array II (Medium) using an in-place two-pointer approach. This journey isn’t about solving the hardest problems daily, but about showing up consistently and improving step by step. Problems like this strengthen core fundamentals of arrays and in-place manipulation. Today’s problem reinforced key concepts like: • Using two pointers to track valid positions • Maintaining constraints (at most twice occurrence) • Performing operations in-place with O(1) space • Writing clean and efficient logic The idea is simple — allow each element at most twice by comparing with the element at index i-2, ensuring the array stays valid 💡 Every problem adds a new layer to problem-solving skills, and that’s where real growth happens 💯 ✅ Better understanding of array manipulation ✅ Improved in-place optimization skills ✅ More confidence in DSA concepts Still a long way to go, but progress is progress 🚀 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #Algorithms #Programming #DeveloperJourney #KeepCoding
To view or add a comment, sign in
-
-
🚀 LeetCode Weekly Progress Update This week I focused on strengthening my understanding of: • Binary Trees (Same Tree, Inorder Traversal) • Linked List manipulation • Backtracking (Combination Sum I & II) • String problems (Multiply Strings) • Array operations (Merge Sorted Array) 📊 Current Stats: Total Problems Solved: 49 Easy: 19 Medium: 24 Hard: 6 Acceptance Rate: 58.6% Key Learning: Backtracking problems improved my recursive thinking. Tree traversal strengthened my understanding of DFS patterns. Consistency is the real game-changer in DSA preparation. Next Goal: → Improve speed → Start more Hard problems → Revise recursion patterns #LeetCode #DSA #Java #SoftwareDevelopment #Consistency
To view or add a comment, sign in
-
Day 15 | LeetCode Daily Solved LeetCode Problem #172 – Factorial Trailing Zeroes Concept: • Counting factors of 5 in factorials • Mathematical optimization over brute force Key Learnings: • Trailing zeroes depend on the number of (2,5) pairs • Counting factors is more efficient than computing factorials Halfway to 30 days — focusing on consistency and clarity in problem solving. Submission link: https://lnkd.in/geCnvxpw #LeetCode #DSA #Math #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Solved LeetCode 338: Counting Bits 🚀 I recently worked on the Counting Bits problem, where the task is: 👉 Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i. 🔹 My Approach: Initialize an array of size n+1. For each number from 1 to n: Copy the number into a temporary variable. Use a loop to repeatedly divide by 2, counting the remainder (num % 2) each time. Store the total count of 1’s in the array. Return the final array. 📊 Complexity Analysis: Time Complexity: O(n log n) Each number requires about log(i) steps to process its binary digits. Space Complexity: O(n) Only the result array of size n+1 is used. ✨ Key Learning: This problem strengthened my understanding of binary representation and iterative problem solving. It was a great reminder that even simple bitwise operations can unlock powerful solutions. #LeetCode #Java #ProblemSolving #BackendDevelopment #CodingJourney #BitManipulation
To view or add a comment, sign in
-
-
🚀 Day 27 of My DSA Challenge – Counting Bits (LeetCode) Today’s problem was about counting the number of 1’s in the binary representation for every number from 0 to n. At first glance, a brute-force solution gives a time complexity of O(n log n). But the real challenge? Solving it in O(n) without using any built-in functions. 🔎 Key Insight: We can use Dynamic Programming with this relation: ans[i] = ans[i >> 1] + (i & 1) ✔ i >> 1 removes the last bit ✔ i & 1 checks if the last bit is 1 This allows us to build the solution in a single pass. 💡 What I learned today: Bit manipulation can drastically improve performance. Many problems that look mathematical can be solved using pattern observation. Understanding binary representation deeply helps in cracking medium-level questions easily. Consistency > Motivation. One problem a day, getting 1% better every day. #DSA #LeetCode #Java #CodingJourney #ProblemSolving #BitManipulation
To view or add a comment, sign in
-
-
Day 7 of Daily DSA 🚀 Solved LeetCode 238: Product of Array Except Self ✅ 🔍 Approach: Built the solution using prefix and suffix products. First pass stores left-side products in the result array Second pass multiplies them with right-side products using a single variable No division used, and handled edge cases like zeros efficiently. ⏱ Complexity: • Time: O(n) • Space: O(1) extra space (output array excluded) 📊 LeetCode Stats: • Runtime: 2 ms (Beats 94.19%) • Memory: 72.23 MB This problem is a great example of how space optimization + traversal logic can turn a seemingly tricky problem into a clean, interview-ready solution. #DSA #LeetCode #Java #Arrays #PrefixSum #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
32).😁 🚀 LeetCode #219 – Contains Duplicate II | Arrays | Hashing | C++ Solved LeetCode Problem 219, which focuses on index-based duplicate detection using hashing. 🔍 Problem Overview Given an integer array nums and an integer k, determine whether there exist two distinct indices i and j such that: nums[i] == nums[j] |i - j| ≤ k If such indices exist, return true; otherwise, return false. 📌 Example Input: nums = [1,2,3,1], k = 3 Output: true It uses a **sliding window** approach combined with an **unordered set** to efficiently track elements. As the loop iterates through the array, the set stores only the last `k` elements seen so far. If the current index `i` becomes greater than `k`, the element that lies just outside the window (`nums[i - k - 1]`) is removed from the set to maintain the window size. Before inserting the current element `nums[i]`, the code checks whether it already exists in the set; if it does, that means the same value has appeared within the last `k` indices, so the function immediately returns `true`. -> If no such duplicate is found throughout the loop, the function returns `false`. !!!!!!!!! This approach runs in **O(n)** time and uses **O(k)** extra space, making it efficient for large inputs !!!!!!!!!! ⚙️ Complexity Analysis ⏱ Time Complexity: O(n) 💾 Space Complexity: O(n) 📌 Language: C++ 📌 Platform: LeetCode #LeetCode #CPlusPlus #DSA #Arrays #Hashing #ProblemSolving #CodingPractice #CompetitiveProgramming #SoftwareEngineering #ComputerScience #CSStudents #BCA #BCAStudents #InterviewPreparation #DailyCoding #LearningToCode #DeveloperJourney #TechJourney #Consistency #CodingLife
To view or add a comment, sign in
-
-
🚀 Day 9/100 – Find First and Last Position of Element in Sorted Array | LeetCode (Medium) Continuing my #100DaysOfChallenges journey. Today’s problem strengthened my understanding of Binary Search variations. 🧩 Problem: Given a sorted array, find the starting and ending position of a given target value. If the target is not found, return [-1, -1]. Constraint: Must run in O(log n) time. 🧠 Approach Used: Modified Binary Search (Lower Bound + Upper Bound) Instead of scanning the array linearly, I applied: ✔️ Binary Search to find the first occurrence ✔️ Binary Search to find the last occurrence ✔️ Carefully adjusted search boundaries ✔️ Maintained O(log n) complexity Core idea: When target found → continue searching left (for first position) When target found → continue searching right (for last position) ⏱ Complexity: Time: O(log n) Space: O(1) 🎯 Key Learning: Binary Search has many powerful variations Boundary conditions are critical Interview questions often test edge-case precision This problem strengthened my: 👉 Algorithm optimization thinking 👉 Boundary handling skills 👉 Confidence with search-based problems 91 days to go 🚀 #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #CodingJourney #TechGrowth #AIML
To view or add a comment, sign in
-
-
🚀 Day 19 of My LeetCode Challenge Today’s problem: Find All Duplicates in an Array (LeetCode 442) 🔍 Problem Summary: Given an array of integers where each value appears once or twice and lies between 1 and n, return all elements that appear twice. 🧠 Key Insight: Since numbers are in the range 1 to n, we can use the array itself to track visited elements by marking indices negative. ⚙ Approach (O(n) time | O(1) space): ✔ Traverse the array ✔ Use value → index mapping ✔ If index already negative → duplicate found ⏱ Complexity: ✅ Time: O(n) ✅ Space: O(1) 📌 What I Learned Today: Using index mapping for in-place marking How constraints help optimize space Recognizing patterns for array-based hashing #Day19 #LeetCode #Java #DSA #CodingChallenge #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