Understand Number Of Good Nodes Problem

Problem Name: Number Of Good Nodes
Problem Description:

Number of Good Nodes Problem

Problem Statement:

In a binary tree, a node is considered "good" if the value of the node is greater than or equal to the maximum value encountered on the path from the root to that node. Your task is to find the number of good nodes in the binary tree.

Definition:

  • Good Node: A node is good if, along the path from the root to this node, no node has a value greater than the value of the current node.

Approach

We will use a Depth-First Search (DFS) approach to traverse the tree and calculate the number of good nodes.

Recursive DFS Function:

  1. Parameters:

    • node: Current node in the tree.
    • max_val: The maximum value encountered along the path from the root to the current node.
  2. Base Case:

    • If the current node is null, return 0 since no node exists.
  3. Processing Current Node:

    • If the current node's value is greater than or equal to max_val, it is a good node.
    • Increase the count of good nodes by 1.
  4. Recursive Call:

    • Perform DFS on both the left and right children, updating the max_val with the maximum value between max_val and the current node's value.
  5. Return the Total Count:

    • Return the total number of good nodes by adding the results from the left and right subtrees, including the current node if it's good.

Test Cases

Test Case 1: Simple Tree

Input:

8

/
5 9 / \
4 7 10 /
6 15

Output:
7

Explanation:
The good nodes are 8, 9, 10, 6, and 15, since their values are greater than or equal to the maximum encountered along the path from root.


Time Complexity:

  • Time Complexity: O(N) where N is the number of nodes in the tree, because we visit each node once.

Space Complexity:

  • Space Complexity: O(H) where H is the height of the tree. This is the space used by the recursive stack in the DFS traversal.
Category:
  • Tries
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/count-the-number-of-good-nodes/description/

Online IDE

Scroll down for output
Java
Output:

Loading component...

Loading component...

Tracking code (View only. In case you want to track the code, click this button):
Main Function:

Main Function is not defined.

Helper Function:

public static int countGoodNodes(vector<vector<int>>& edges) {

int n = edges.size() + 1;

adj.resize(n);

for (const auto& edge : edges) {

int u = edge[0], v = edge[1];

adj[u].push_back(v);

adj[v].push_back(u);

}

ans = 0;

dfs(0, -1);

return ans;

Utility Functions and Global variables:

vector<vector<int>> adj;

int ans;

int dfs(int node, int parent) {

int totalNodes = 0;

bool isGood = true;

int subtreeSize = -1;

for (int neighbor : adj[node]) {

if (neighbor == parent){

continue;

}

int currentSize = dfs(neighbor, node);

if (subtreeSize == -1) {

subtreeSize = currentSize;

}

else {

if (currentSize != subtreeSize) {

isGood = false;

}

}

totalNodes += currentSize;

}

if (isGood){

ans++;

}

return totalNodes + 1;

}//function end