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.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
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.
We will use a Depth-First Search (DFS) approach to traverse the tree and calculate the number of good nodes.
Parameters:
node: Current node in the tree.max_val: The maximum value encountered along the path from the root to the current node.Base Case:
null, return 0 since no node exists.Processing Current Node:
max_val, it is a good node.Recursive Call:
max_val with the maximum value between max_val and the current node's value.Return the Total Count:
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.
O(N) where N is the number of nodes in the tree, because we visit each node once.O(H) where H is the height of the tree. This is the space used by the recursive stack in the DFS traversal.🎯 Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
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 AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
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.