dfs
that takes a node and the maximum value seen so far as arguments.count
to 0.count
.dfs
for the left child, passing the greater of the node's value or the current maximum as the new maximum.dfs
for the right child, similarly updating the maximum.count
plus the counts from the left and right subtree calls.dfs
with the root node and the smallest possible integer value (representing the maximum value seen so far) and return the result.