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

2024/02/29閱讀時間約 7 分鐘


題目敘述

題目會給定我們一顆二元樹的根結點,要求我們計算這棵樹的好結點Good node有多少個?

好結點Good node的定義:
某個節點v是好結點假如從Root node根結點 到 結點v沿途的節點值都小於等於節點v的節點值

如果還是覺得很模糊,看下方的測試範例就可以很清楚了解題目的意思。


題目的原文敘述


測試範例

Example 1:

raw-image

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:

raw-image

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的定義:
以行動支持創作者!付費即可解鎖
本篇內容共 3023 字、0 則留言,僅發佈於Leetcode 精選75題 上機考面試題 詳解你目前無法檢視以下內容,可能因為尚未登入,或沒有該房間的查看權限。
43會員
287內容數
由有業界實戰經驗的演算法工程師, 手把手教你建立解題的框架, 一步步寫出高效、清晰易懂的解題答案。 著重在讓讀者啟發思考、理解演算法,熟悉常見的演算法模板。 深入淺出地介紹題目背後所使用的演算法意義,融會貫通演算法與資料結構的應用。 在幾個經典的題目融入一道題目的多種解法,或者同一招解不同的題目,擴展廣度,並加深印象。
留言0
查看全部
發表第一個留言支持創作者!