<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?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


          思路看上面,代碼實現(xiàn):

          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:將有序數(shù)組轉換為二叉搜索樹
          LeetCode刷題實戰(zhàn)109:有序鏈表轉換二叉搜索樹
          LeetCode刷題實戰(zhàn)110:平衡二叉樹
          LeetCode刷題實戰(zhàn)111:二叉樹的最小深度
          LeetCode刷題實戰(zhàn)112:路徑總和

          瀏覽 24
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  操逼网站免费在线观看 | 国产精品深夜福利 | 国内成人做爱视频播放 | 亚洲最大视频 | 少女爱操B |