1. 程式人生 > >156. Binary Tree Upside Down反轉二叉樹

156. Binary Tree Upside Down反轉二叉樹

反轉 rec 數據結構 bin 總結 sid 特殊 ren psi

[抄題]:

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.

Example:

Input: [1,2,3,4,5]

    1
   /   2   3
 / 4   5

Output: return the root of the binary tree [4,5,2,#,#,3,1]

   4
  /  5   2
    /    3   1  

[暴力解法]:

時間分析:

空間分析:

[優化後]:

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思維問題]:

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

二叉樹中用left/right是recursive,類似圖中的公式

一個個節點去寫是iterative,類似圖中的for循環

[一句話思路]:

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

[畫圖]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[總結]:

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

[算法思想:叠代/遞歸/分治/貪心]:

[關鍵模板化代碼]:

[其他解法]:

[Follow Up]:

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

[代碼風格] :

[是否頭一次寫此類driver funcion的代碼] :

[潛臺詞] :

156. Binary Tree Upside Down反轉二叉樹