Advertisement

Count Good Nodes in Binary Tree - LeetCode 1448 Solution

Count Good Nodes in Binary Tree - Complete Solution Guide

Count Good Nodes in Binary Tree is LeetCode problem 1448, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Example 1: Input: root = [3,1,4,3,null,1,5] Output: 4 Explanation: Nodes in blue are good . Root Node (3) is always a good node. Node 4 -> (3,4) is the maximum value in the path starting from the root. Node 5 -> (3,4,5) is the maximum value in the path Node 3 -> (3,1,3) is the maximum value in the path. Exa

Detailed Explanation

The problem asks us to traverse a binary tree and count the number of 'good' nodes. A node is considered 'good' if, on the path from the root to that node, there are no nodes with a value greater than the node's value. In simpler terms, the node's value must be the maximum value seen so far along the path from the root. We are given the root node of the binary tree and must return the total number of good nodes.

Solution Approach

The solution uses a Depth-First Search (DFS) traversal of the binary tree. For each node, it checks if the node's value is greater than or equal to the maximum value seen so far on the path from the root. If it is, the node is a 'good' node, and the count is incremented. The maximum value is then updated to be the maximum of the current maximum and the node's value. The DFS continues recursively down the left and right subtrees, passing the updated maximum value to each recursive call.

Step-by-Step Algorithm

  1. Step 1: Initialize a counter to 0 to store the number of good nodes.
  2. Step 2: Define a recursive helper function `dfs(node, max_val)` where `node` is the current node being visited and `max_val` is the maximum value seen from root to `node`.
  3. Step 3: Base case for the recursion: If the current `node` is `None`, return 0 (no good nodes found in an empty subtree).
  4. Step 4: Check if the current `node.val` is greater than or equal to `max_val`. If it is, increment the counter by 1 (it's a good node).
  5. Step 5: Update `max_val` to be the maximum of the current `max_val` and `node.val`.
  6. Step 6: Recursively call `dfs(node.left, max_val)` and add the result to the counter.
  7. Step 7: Recursively call `dfs(node.right, max_val)` and add the result to the counter.
  8. Step 8: Return the final count.
  9. Step 9: Call the `dfs` function starting from the `root` node with an initial `max_val` smaller than any possible node value (e.g., -10001).
  10. Step 10: Return the value returned by the `dfs` function as the result.

Key Insights

  • Insight 1: We need to keep track of the maximum value encountered so far along the path from the root to the current node.
  • Insight 2: Depth-First Search (DFS) is a natural fit for traversing the tree and maintaining the path information.
  • Insight 3: We can use a helper function that takes the current node and the maximum value seen so far as input.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

This problem involves: Tree, Depth-First Search, Breadth-First Search, Binary Tree.

Companies

Asked at: Docusign, josh technology.