1448. Count Good Nodes in Binary Tree
Medium
Previous1443. Minimum Time to Collect All Apples in a TreeNext1450. Number of Students Doing Homework at a Given Time
Last updated
Medium
Last updated
Given a binary tree root
, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.
Return the number of good nodes in the binary tree.
Example 1:
Example 2:
Example 3:
Constraints:
The number of nodes in the binary tree is in the range [1, 10^5]
.
Each node's value is between [-10^4, 10^4]
.
記錄到從root到目前節點的最大值即可,如果目前節點的值比最大值大,答案+1