Number Of Good Nodes — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Number Of Good Nodes
Detailed explanation and reference materials
Problem Overview

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.
— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
tries
java
Reference Link
https://leetcode.com/problems/count-the-number-of-good-nodes/description/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.