LeetCode #1448 Medium

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.

Constraints
  • The number of nodes in the binary tree is in the range [1, 10⁵].
  • Each node's value is between [-10⁴, 10⁴].
treedfs
Open on LeetCode ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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?

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def goodNodes(self, root: TreeNode) -> int:
▶3 def dfs(node, high):
▶4 if not node:
▶5 return 0
▶6 good = 1 if node.val >= high else 0
▶7 high = max(high, node.val)
▶8 return good + dfs(node.left, high) + dfs(node.right, high)
▶9 
▶10 return dfs(root, float("-inf"))
treegoodnot goodcreturned314315node−∞path maxdfs(root 3, −∞)
high−∞nothing above the root
Start at the root with 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.
treegoodnot goodcreturned3143153node≥−∞path max→3new max3 ≥ −∞ → good
node3
high−∞largest above
good1
The root is good: 3 ≥ −∞. Its children get high = 3.
treegoodnot goodcreturned3143151node<3path max→3new max1 < 3 → not good
node1
high3largest above
good0
1 is not good: 3 sits above it on the blue path and is greater. Its children get high = 3.
treegoodnot goodcreturned3143153node≥3path max→3new max3 ≥ 3 → good
node3
high3largest above
good1
3 is good: the largest value above it (blue path) is 3, equal to it, and equal still means nothing above is greater. Its children get high = 3.
treegoodnot goodcreturned314315node3path maxleft of 3 is empty → return 0
nodeempty
return0
Node 3 has no left child. An empty subtree holds no good nodes, so the call returns 0.
treegoodnot goodcreturned314315node3path maxright of 3 is empty → return 0
nodeempty
return0
Node 3 has no right child. An empty subtree holds no good nodes, so the call returns 0.
treegoodnot goodcreturned31431151this node+0left+0right=1returnsreturn 1 to node 1
good here1
left0
right0
return1
Both subtrees of 3 are finished. Its count is its own 1 plus 0 from the left and 0 from the right: 1, handed up to 1.
treegoodnot goodcreturned3143115node3path maxright of 1 is empty → return 0
nodeempty
return0
Node 1 has no right child. An empty subtree holds no good nodes, so the call returns 0.
treegoodnot goodcreturned311431150this node+1left+0right=1returnsreturn 1 to node 3
good here0
left1
right0
return1
Both subtrees of 1 are finished. Its count is its own 0 plus 1 from the left and 0 from the right: 1, handed up to 3.
treegoodnot goodcreturned311431154node≥3path max→4new max4 ≥ 3 → good
node4
high3largest above
good1
4 is good: the largest value above it (blue path) is 3, and 4 is at least that big. Its children get high = 4, raised to 4.
treegoodnot goodcreturned311431151node<4path max→4new max1 < 4 → not good
node1
high4largest above
good0
1 is not good: 4 sits above it on the blue path and is greater. Its children get high = 4.
treegoodnot goodcreturned31143115node4path maxleft of 1 is empty → return 0
nodeempty
return0
Node 1 has no left child. An empty subtree holds no good nodes, so the call returns 0.
treegoodnot goodcreturned31143115node4path maxright of 1 is empty → return 0
nodeempty
return0
Node 1 has no right child. An empty subtree holds no good nodes, so the call returns 0.
treegoodnot goodcreturned3114311050this node+0left+0right=0returnsreturn 0 to node 4
good here0
left0
right0
return0
Both subtrees of 1 are finished. Its count is its own 0 plus 0 from the left and 0 from the right: 0, handed up to 4.
treegoodnot goodcreturned3114311055node≥4path max→5new max5 ≥ 4 → good
node5
high4largest above
good1
5 is good: the largest value above it (blue path) is 4, and 5 is at least that big. Its children get high = 5, raised to 5.
treegoodnot goodcreturned311431105node5path maxleft of 5 is empty → return 0
nodeempty
return0
Node 5 has no left child. An empty subtree holds no good nodes, so the call returns 0.
treegoodnot goodcreturned311431105node5path maxright of 5 is empty → return 0
nodeempty
return0
Node 5 has no right child. An empty subtree holds no good nodes, so the call returns 0.
treegoodnot goodcreturned31143110511this node+0left+0right=1returnsreturn 1 to node 4
good here1
left0
right0
return1
Both subtrees of 5 are finished. Its count is its own 1 plus 0 from the left and 0 from the right: 1, handed up to 4.
treegoodnot goodcreturned311423110511this node+0left+1right=2returnsreturn 2 to node 3
good here1
left0
right1
return2
Both subtrees of 4 are finished. Its count is its own 1 plus 0 from the left and 1 from the right: 2, handed up to 3.
treegoodnot goodcreturned3411423110511this node+1left+2right=4returnsroot returns 4 good nodes
good here1
left1
right2
return4
Both subtrees of 3 are finished. Its count is its own 1 plus 1 from the left and 2 from the right: 4, which covers the whole tree.
treegoodnot goodcreturned3411423110514good nodesreturn 4
answer4
4 good nodes. Each node was checked once against the largest value on its own path, which the recursion carried down instead of walking back up.
05

Common pitfalls

Comparing with the parent only

✗ Wrong
good = 1 if node.val >= parent.val else 0
✓ Right
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

✗ Wrong
self.high = max(self.high, node.val)
dfs(node.left)
dfs(node.right)
✓ 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 >=

✗ Wrong
good = 1 if node.val > high else 0
✓ Right
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

✗ Wrong
return dfs(root, 0)
✓ Right
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.

06

Complexity

Time
O(n)
Space
O(h)
Each node is visited once with O(1) work. The recursion is as deep as the tree is tall: about log₂ n for a balanced tree, up to n for a tree shaped like a line.
07

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.