Count Good Nodes in Binary Tree - Problem

Imagine you're exploring a binary tree where each node represents a checkpoint on your journey from the root to various destinations. A node is considered "good" if, throughout your entire path from the root to that node, you never encountered a value greater than the current node's value.

Your mission: Count how many "good" nodes exist in the entire binary tree.

What makes a node "good"?

  • Start from the root and trace the path to the target node
  • If no node along this path has a value greater than the target node's value, then it's good!
  • Note: A node can equal values on the path and still be considered good

Input: The root of a binary tree
Output: An integer representing the count of good nodes

Example: In a tree with root value 3, if we reach a node with value 4 and the path was [3 โ†’ 4], then node 4 is good because 3 โ‰ค 4.

Input & Output

example_1.py โ€” Standard Binary Tree
$ Input: root = [3,1,4,3,null,1,5]
โ€บ Output: 4
๐Ÿ’ก Note: Root node (3) is always good. Path to left child (1): [3,1], 3 > 1 so not good. Path to right child (4): [3,4], 3 โ‰ค 4 so it's good. Path to left grandchild (3): [3,1,3], max so far is 3, and 3 โ‰ฅ 3 so it's good. Path to (1): [3,4,1], max is 4, 1 < 4 so not good. Path to (5): [3,4,5], max is 4, 5 โ‰ฅ 4 so it's good.
example_2.py โ€” Single Node Tree
$ Input: root = [1]
โ€บ Output: 1
๐Ÿ’ก Note: Root node is always considered good since there are no ancestors with greater values.
example_3.py โ€” Decreasing Values
$ Input: root = [5,4,3,2,1]
โ€บ Output: 5
๐Ÿ’ก Note: All nodes are good because we never encounter a value greater than any node along its path from root. The path values are always decreasing or equal.

Visualization

Tap to expand
3โœ“1โœ—4โœ“3โœ“5โœ“๐Ÿ”๏ธ Good Node Mountain Trailโœ“ Good checkpoint (elevation โ‰ฅ max so far)โœ— Bad checkpoint (elevation < max so far)
Understanding the Visualization
1
Start at Base Camp
Begin at root (elevation 3) - it's always good since no previous elevations
2
Track Peak Elevation
As you hike, remember the highest elevation seen so far (max = 3)
3
Check Each Checkpoint
At each new checkpoint, compare its elevation with your recorded peak
4
Count Good Checkpoints
If current elevation โ‰ฅ peak elevation, it's a good checkpoint
Key Takeaway
๐ŸŽฏ Key Insight: Instead of retracing every path, carry the 'peak elevation' with you as you hike through the tree once!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all n nodes in the tree

n
2n
โœ“ Linear Growth
Space Complexity
O(h)

Recursion stack depth equals tree height h, O(log n) for balanced tree, O(n) for skewed tree

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the binary tree is in the range [1, 105]
  • Each node's value is between -104 and 104
  • Tree nodes can have duplicate values
  • A node is good if its value is greater than or equal to all ancestor values
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
89.4K Views
High Frequency
~18 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen