1. ?LeetCode刷題實戰(zhàn)156:上下翻轉二叉樹

        共 2444字,需瀏覽 5分鐘

         ·

        2021-01-16 13:56

        算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問題叫做?上下翻轉二叉樹(這題Leetcode需要會員才能看),我們先來看題面:
        https://leetcode-cn.com/problems/binary-tree-upside-down

        Given a binary tree in which all right nodes are either leaf nodes with sibling nodes (left nodes with the same parent nodes) or empty, flip the binary tree up and down and turn it into a tree, and the original right nodes will be converted into left leaf nodes. Returns the new root.? ?

        題意

        給定一個二叉樹,其中所有的右節(jié)點要么是具有兄弟節(jié)點(擁有相同父節(jié)點的左節(jié)點)的葉節(jié)點,要么為空,將此二叉樹上下翻轉并將它變成一棵樹, 原來的右節(jié)點將轉換成左葉節(jié)點。返回新的根。

        樣例

        解題

        https://blog.csdn.net/qq_32424059/article/details/93920527


        思路1:遞歸

        對于root而言,root應該成為root.left的右孩子,root.right應該成為root.left的左孩子。最后返回的是root.left處理之后的返回值。

        class?Solution(object):
        ????def?upsideDownBinaryTree(self, root):
        ????????"""
        ????????:type root: TreeNode
        ????????:rtype: TreeNode
        ????????"""

        ????????#每一個節(jié)點變成其左孩子的右節(jié)點
        ????????if?not?root or?(not?root.left and?not?root.right):
        ????????????return?root
        ?
        ????????newroot = self.upsideDownBinaryTree(root.left)
        ????????# self.upsideDownBinaryTree(root.right) 右孩子不用處理 因為要么不存在要么是葉節(jié)點
        ????????
        ????????root.left.left = root.right
        ????????root.left.right = root
        ????????root.left = None
        ????????root.right = None
        ?
        ????????return?newroot


        思路2:迭代
        對于任意node,假設我們已經(jīng)知道了它的父節(jié)點和兄弟節(jié)點,
        先把node.left, node.right備份好,
        然后node.left 就是兄弟節(jié)點, node.right就是父節(jié)點。
        下一次循環(huán)處理備份好的原來的node.left。

        class?Solution(object):
        ????def?upsideDownBinaryTree(self, root):
        ????????"""
        ????????:type root: TreeNode
        ????????:rtype: TreeNode
        ????????"""

        ????????#每一個節(jié)點變成其左孩子的右節(jié)點
        ????????if?not?root or?(not?root.left and?not?root.right):
        ????????????return?root
        ?
        ????????parent, sibling = None, None
        ????????while?root:
        ????????????tmp = root.left
        ????????????root.left = sibling
        ????????????
        ????????????sibling = root.right
        ????????????root.right = parent
        ????????????
        ????????????parent = root
        ????????????root = tmp
        ????????????
        ????????return?parent

        好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉發(fā)吧,你們的支持是我最大的動力。

        上期推文:

        LeetCode1-140題匯總,希望對你有點幫助!
        LeetCode刷題實戰(zhàn)141:環(huán)形鏈表
        LeetCode刷題實戰(zhàn)142:環(huán)形鏈表 II
        LeetCode刷題實戰(zhàn)143:重排鏈表
        LeetCode刷題實戰(zhàn)144:二叉樹的前序遍歷
        LeetCode刷題實戰(zhàn)145:二叉樹的后序遍歷
        LeetCode刷題實戰(zhàn)146:LRU 緩存機制
        LeetCode刷題實戰(zhàn)147:對鏈表進行插入排序
        LeetCode刷題實戰(zhàn)148:排序鏈表
        LeetCode刷題實戰(zhàn)149:直線上最多的點數(shù)
        LeetCode刷題實戰(zhàn)150:逆波蘭表達式求值
        LeetCode刷題實戰(zhàn)151:翻轉字符串里的單詞
        LeetCode刷題實戰(zhàn)152:乘積最大子數(shù)組
        LeetCode刷題實戰(zhàn)153:尋找旋轉排序數(shù)組中的最小值
        LeetCode刷題實戰(zhàn)154:尋找旋轉排序數(shù)組中的最小值 II
        LeetCode刷題實戰(zhàn)155:最小棧


        瀏覽 108
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
          
          

            1. 影音先锋色图 | 91国内产香蕉 | 在线操B| 风骚少妇视频 | 成人免费毛片 高清视频 |