題目會給定我們一顆二元樹的根結點,要求我們計算這棵樹的好結點Good node有多少個?
好結點Good node的定義:
某個節點v是好結點,假如從Root node根結點 到 結點v沿途的節點值都小於等於節點v的節點值。
如果還是覺得很模糊,看下方的測試範例就可以很清楚了解題目的意思。
Example 1:
Input: root = [3,1,4,3,null,1,5]
Output: 4
Explanation: Nodes in blue are good.
Root Node (3) is always a good node.
Node 4 -> (3,4) is the maximum value in the path starting from the root.
Node 5 -> (3,4,5) is the maximum value in the path
Node 3 -> (3,1,3) is the maximum value in the path.
Example 2:
Input: root = [3,3,null,4,2]
Output: 3
Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.
Example 3:
Input: root = [1]
Output: 1
Explanation: Root is considered as good.
Constraints:
[1, 10^5]
.結點總數目介於 1 ~ 十萬之間。
[-10^4, 10^4]
.節點值都介於 負一萬 ~ 正一萬 之間。
從題目的定義來看
好結點Good node的定義:
某個節點v是好結點,假如從Root node根結點 到 結點v沿途的節點值都小於等於節點v的節點值。