Count Good Nodes in Binary Tree
Count Good Nodes in Binary Tree is LeetCode 1448 (Medium). Given the root of a binary tree, a node X is good if no node on the path from the root down to X has a value greater than X. Return the number of good nodes.
- The root is always good: its path holds only itself.
- Values can repeat and can be negative.
The tree can have 10⁵ nodes, so each node should be checked in O(1), not by walking back up its path.
- The number of nodes in the binary tree is in the range [1, 10⁵].
- Each node's value is between [-10⁴, 10⁴].
Intuition
Whether a node is good depends on one number: the largest value on the path above it. If the node is at least that large, nothing on its path beats it.
That number is easy to hand down. Going from a node to its child, the path grows by exactly that node, so the child's path maximum is the larger of the node's own path maximum and the node's value.
So a DFS carries the path maximum as a parameter: each call compares, counts, and passes an updated maximum to its children. Every node is checked once, in O(1), and the counts from both subtrees add up to the answer.
"Something about the path from the root to this node" means top-down state: pass it into the recursion as a parameter, the way Validate Binary Search Tree passes bounds and Path Sum passes the remaining sum.
Approach
Before reading on: in [3,1,4,3,null,1,5], which nodes are good? For each one, what is the largest value on the path above it, and how does a child get that number from its parent?
Pass the path maximum into each call
dfs(node, high) receives high, the largest value from the root down to this node's parent. Start with dfs(root, -infinity): nothing is above the root, so it always counts.
Count the node if nothing above beats it
If node is empty, return 0. Otherwise the node is good when node.val >= high: a value equal to the maximum still has nothing greater above it.
Raise the maximum and visit both children
Set high = max(high, node.val) and call dfs(node.left, high) and dfs(node.right, high). Because high is a parameter, each child gets its own copy, so values from the left subtree never reach the right one.
Add up the counts
Return this node's 1 or 0 plus the two children's counts. The root's call returns the number of good nodes in the whole tree.
Code in Python, C++ and Java
high = −∞. high will always be the largest value on the path above the current node. Nothing is above the root, so any root value passes the check.high = 3.high = 3.high = 3.high = 4, raised to 4.high = 4.high = 5, raised to 5.high = −∞. high will always be the largest value on the path above the current node. Nothing is above the root, so any root value passes the check.high = 3.high = 3.high = 4, raised to 4.high = 3.high = −∞. high will always be the largest value on the path above the current node. Nothing is above the root, so any root value passes the check.high = 1.Common pitfalls
Comparing with the parent only
good = 1 if node.val >= parent.val else 0
good = 1 if node.val >= high else 0
A node can beat its parent and still lose to a node higher up. In [10,3,null,7], 7 is larger than its parent 3, but 10 is on its path, so 7 is not good.
Keeping one shared maximum for the whole tree
self.high = max(self.high, node.val) dfs(node.left) dfs(node.right)
high = max(high, node.val) dfs(node.left, high) dfs(node.right, high)
A shared field still holds the left subtree's largest value when the right subtree is visited, although those nodes are not on the right side's paths. In [1,5,2], node 2 would be compared with 5 and missed.
Using > instead of >=
good = 1 if node.val > high else 0
good = 1 if node.val >= high else 0
A node equal to the largest value above it has nothing greater on its path, so it is good. In the second LeetCode example, [3,3,null,4,2], the second 3 must count; > returns 2 instead of 3.
Starting the maximum at 0
return dfs(root, 0)
return dfs(root, float("-inf"))Values go down to -10⁴. With 0 as the start, a negative root fails node.val >= 0 and is not counted, although the root is always good.
Complexity
Count Good Nodes in Binary Tree FAQ
Can this be done without recursion?
Yes. Put (root, -infinity) on a stack or queue. Each time you take out (node, high), count the node if node.val >= high, then push each child with max(high, node.val). It is the same check in the same O(n) time, and it avoids a deep call stack on a tree shaped like a long line.