Count Nodes That Are No Smaller Than Every Ancestor Above Them
Last updated: November 29, 2026
Quick Overview
In a binary tree of quarterly metric values, a node is a record node if no node on the path from the root to it has a strictly greater value. Count the record nodes.
0
0
0 solved
In a binary tree of quarterly metric values, a node is a record node if no node on the path from the root to it has a strictly greater value. Count the record nodes.
A Google phone screen question that checks state passed down a DFS. The interviewer wants the path maximum carried as a parameter rather than storing full paths, and an iterative version to show stack fluency.
What the Interviewer Expects
- Carry the running path maximum down the tree
- Count a node when its value is at least the path maximum
- Update the maximum for children
- Implement iteratively with a stack of (node, max) pairs
- State O(n) time and O(h) space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Count nodes that are strictly greater than every ancestor.
- Return the record nodes on the longest root-to-leaf path only.
- Count nodes that are no smaller than every descendant instead.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: root. Output: number of nodes x such that x.val >= max(values on the root-to-x path). The root always counts. Values may be negative, so start the running maximum from the root's own value ra...
High-Level Design
DFS with each stack entry carrying the node and the maximum value on the path above it. When a node's value is at least that maximum, increment the count. Children receive max(path_max, node.val). B...