Count Good Nodes in Binary Tree with DFS

🚀 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

  • graphical user interface, text, application

To view or add a comment, sign in

Explore content categories