2024-02-29|閱讀時間 ‧ 約 0 分鐘

圖論變化題: 計算好節點Good node的數目_Leetcode 1448_Leetcode精選75題


題目敘述

題目會給定我們一顆二元樹的根結點,要求我們計算這棵樹的好結點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:

  • The number of nodes in the binary tree is in the range [1, 10^5].

結點總數目介於 1 ~ 十萬之間。

  • Each node's value is between [-10^4, 10^4].

節點值都介於 負一萬 ~ 正一萬 之間。


演算法

從題目的定義來看

好結點Good node的定義:
分享至
成為作者繼續創作的動力吧!
Leetcode 國際版精選75題 上機考面試題 詳解 裡面包含: 1. 內涵題意解析 2. 演算法建造 3. python解題程式碼 4. 複雜度分析 5. 關鍵知識點提示
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

發表回應

成為會員 後即可發表留言