1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)114:二叉樹展開為鏈表

        共 2112字,需瀏覽 5分鐘

         ·

        2020-12-06 20:44

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

        今天和大家聊的問題叫做?二叉樹展開為鏈表,我們先來看題面:
        https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/
        Given a binary tree, flatten it to a linked list in-place.

        題意


        給定一個二叉樹,原地將它展開為一個單鏈表。

        樣例

        解題

        https://www.cnblogs.com/gongyanzh/p/12901595.html


        將左子樹插入到右子樹的地方

        將原來的右子樹接到左子樹的最右邊節(jié)點

        考慮新的右子樹的根節(jié)點,一直重復上邊的過程,直到新的右子樹為 null


        ? ? ?1
        ???/? \
        ??2???5
        ?/ \? ? ?\
        3??4? ? ?6

        //將 1 的左子樹插入到右子樹的地方
        ????1
        ?????\
        ? ? ? ?2? ? ? 5
        ?????/?\? ? ?\
        ????3???4????6????????
        //將原來的右子樹接到左子樹的最右邊節(jié)點
        ????1
        ?????\
        ??????2??????????
        ?????/ \
        ????3???4??
        ?????????\
        ??????????5
        ???????????\
        ????????????6
        ????????????
        ?//將 2 的左子樹插入到右子樹的地方
        ????1
        ?????\
        ??????2??????????
        ???????\
        ????????3???????4??
        ?????????????????\
        ??????????????????5
        ???????????????????\
        ????????????????????6???
        ????????
        ?//將原來的右子樹接到左子樹的最右邊節(jié)點
        ????1
        ?????\
        ??????2??????????
        ???????\
        ????????3??????
        ?????????\
        ??????????4??
        ???????????\
        ????????????5
        ?????????????\
        ??????????????6


        思路看上面,代碼實現:

        class?Solution?{
        ????public?void?flatten(TreeNode root) {
        ????????//將左子樹接到右子樹 root.right = root.left
        ????????//為了做到這一點,首先要把右子樹移走,移到哪?移到左子樹的右子樹
        ????????//pre = root.left; pre.right = root.right;
        ????????while(root != null){
        ????????????//左子樹為空,直接考慮下個節(jié)點
        ????????????if(root.left == null){
        ????????????????root = root.right;
        ????????????}else{
        ????????????????TreeNode pre = root.left;
        ????????????????while(pre.right != null){
        ????????????????????pre = pre.right;
        ????????????????}
        ????????????????pre.right = root.right;
        ????????????????root.right = root.left;
        ????????????????root.left = null;
        ????????????????root = root.right;
        ????????????}
        ????????}
        ????}
        }

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

        上期推文:
        LeetCode1-100題匯總,希望對你有點幫助!
        LeetCode刷題實戰(zhàn)101:對稱二叉樹
        LeetCode刷題實戰(zhàn)102:二叉樹的層序遍歷
        LeetCode刷題實戰(zhàn)103:二叉樹的鋸齒形層次遍歷
        LeetCode刷題實戰(zhàn)104:二叉樹的最大深度
        LeetCode刷題實戰(zhàn)105:從前序與中序遍歷序列構造二叉樹
        LeetCode刷題實戰(zhàn)106:從中序與后序遍歷序列構造二叉樹
        LeetCode刷題實戰(zhàn)107:二叉樹的層次遍歷 II
        LeetCode刷題實戰(zhàn)108:將有序數組轉換為二叉搜索樹
        LeetCode刷題實戰(zhàn)109:有序鏈表轉換二叉搜索樹
        LeetCode刷題實戰(zhàn)110:平衡二叉樹
        LeetCode刷題實戰(zhàn)111:二叉樹的最小深度
        LeetCode刷題實戰(zhàn)112:路徑總和

        瀏覽 24
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

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

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            在线成人性爱视频 | 国产成人综合在线 | 日本一本视频 | 精品久久久久成人片中文字幕 | 精品视频久久 | 国产igao为爱做激情国外 | 二级黄片免费看 | 国产一级婬乱A片AAA毛多网站 | 欧美性爱超碰 | 麻豆传媒视频免费观看网页 |