Count Good Nodes in Binary Tree
Given the root of a binary tree, a node X is called good if on the path from the root down to X there is no node with a value greater than X. (The root is always good.) Return the total number of good nodes in the tree.
Open official problem prompt ↗Count how many nodes are at least as large as every ancestor on their root-to-node path.
Hike a trail that only branches downward from a summit. You are 'good' at a spot if you can see over every point behind you — no earlier point on your path was taller than where you stand.
- Input
- root = [3,1,4,3,null,1,5]
- Output
- 4
- Why
- Good nodes are root 3, the right child 4, its right child 5, and the 3 that is the left child of node 1 (path max 3, and 3 >= 3).
The number of nodes in the tree is in the range [1, 10^5]-10^4 <= Node.val <= 10^4