1. 程式人生 > >669. Trim a Binary Search Tree修剪二叉搜索樹

669. Trim a Binary Search Tree修剪二叉搜索樹

col 特殊 amp 結構 style 搜索 奇葩 result spa

[抄題]:

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

Example 1:

Input: 
    1
   /   0   2

  L = 1
  R = 2

Output: 
    1
             2

Example 2:

Input: 
    3
   /   0   4
       2
   /
  1

  L = 1
  R = 3

Output: 
      3
     / 
   2   
  /
 1

[暴力解法]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思維問題]:

[一句話思路]:

分左右之後為了保持一路的繼承關系,還是左節點traverse左節點

[輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入):

[畫圖]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分鐘肉眼debug的結果]:

[總結]:

[復雜度]:Time complexity: O() Space complexity: O()

[英文數據結構或算法,為什麽不用別的數據結構或算法]:

[關鍵模板化代碼]:

[其他解法]:

[Follow Up]:

[LC給出的題目變變變]:

[代碼風格] :

669. Trim a Binary Search Tree修剪二叉搜索樹