圖論進階題: 在二元搜索樹BST中刪除節點_Leetcode #450_Leetcode 精選75題

2024/01/31閱讀時間約 10 分鐘

題目敘述

題目會給我們一棵BST二元搜索樹的根結點root,還有一個指定的目標值key。

要求我們在樹中刪除帶有這個key值的節點,並且返回更新過後二元搜索樹的樹根root。


題目的原文敘述


測試範例

Example 1:

raw-image
Input: root = [5,3,6,2,4,null,7], key = 3
Output: [5,4,6,2,null,null,7]
Explanation: Given key to delete is 3. So we find the node with value 3 and delete it.
One valid answer is [5,4,6,2,null,null,7], shown in the above BST.
Please notice that another valid answer is [5,2,6,null,4,null,7] and it's also accepted.

Example 2:

Input: root = [5,3,6,2,4,null,7], key = 0
Output: [5,3,6,2,4,null,7]
Explanation: The tree does not contain a node with value = 0.

Example 3:

Input: root = [], key = 0
Output: []

約束條件

Constraints:

  • The number of nodes in the tree is in the range [0, 10^4].

節點總數目介於0~ 一萬。

請留意邊界條件的處理,題目有可能會給我們一棵空樹喔!
  • -10^5 <= Node.val <= 10^5

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

  • Each node has a unique value.

每個節點值都是獨一無二的,不會彼此重複。

  • root is a valid binary search tree.

root 是這棵合法二元搜索樹的樹根。

  • -10^5 <= key <= 10^5

被刪除的目標值介於 負十萬 ~ 正十萬 之間。


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