国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

leetcode430. Flatten a Multilevel Doubly Linked Li

gxyz / 1581人閱讀

摘要:步驟如下代碼如下思路二循環(huán)上面的思路同樣可以通過循環(huán)的方式來解決。基本步驟如下代碼如下思路減少遍歷次數(shù)之前的兩種思路,都會(huì)出現(xiàn)大量的重復(fù)遍歷,重復(fù)遍歷和葉子節(jié)點(diǎn)的深度成正相關(guān),可以想方法將重復(fù)遍歷的次數(shù)減少。

題目要求
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.

Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.

 

Example:

Input:
 1---2---3---4---5---6--NULL
         |
         7---8---9---10--NULL
             |
             11--12--NULL

Output:
1-2-3-7-8-11-12-9-10-4-5-6-NULL
思路一:遞歸實(shí)現(xiàn)深度優(yōu)先遍歷

從深度優(yōu)先遍歷的角度來看,每次遇到一個(gè)包含子節(jié)點(diǎn)中間雙鏈表節(jié)點(diǎn),就遞歸的調(diào)用展開方法將其展開,并將展開的結(jié)果插入到當(dāng)前節(jié)點(diǎn)的后面。這里需要注意雙鏈表前節(jié)點(diǎn)前后指針的變更。步驟如下:

Step1:
 1---2---3---4---5---6--NULL
         |
         7---8---9---10--NULL
             |
             11--12--NULL

Step2:
1---2---3---4---5---6--NULL
         |
         7---8---11--12--9---10--NULL
         
Step3:
1---2---3---7---8---11--12--9---10--4---5---6--NULL
        

代碼如下:

    public Node flatten(Node head) {
        if(head == null) return head;
        Node tmp = head;
        while(tmp != null) {
            if(tmp.child != null) {
                Node child = flatten(tmp.child);
                tmp.child = null;
                Node next = tmp.next;
                tmp.next = child;
                child.prev = tmp;
                while(child.next != null) {
                    child = child.next;
                }
                child.next = next;
                if(next != null) {
                    next.prev = child;
                }
                tmp = next;
            }else {
                tmp = tmp.next;

            }
        }
        return head;
    }
思路二:循環(huán)

上面的思路同樣可以通過循環(huán)的方式來解決。每遇到一個(gè)有子節(jié)點(diǎn)的雙鏈表節(jié)點(diǎn),就將其子節(jié)點(diǎn)的頭和尾拼接到父節(jié)點(diǎn)的雙鏈表上,使其看上去是一個(gè)新的雙鏈表。再對(duì)雙鏈表的下一個(gè)節(jié)點(diǎn)進(jìn)行判斷。基本步驟如下:

Step1:
 1---2---3---4---5---6--NULL
         |
         7---8---9---10--NULL
             |
             11--12--NULL

Step2:
1---2---3---7---8---9---10---4---5---6--NULL
             |
             11--12--NULL
         
Step3:
1---2---3---7---8---11--12--9---10--4---5---6--NULL
        

代碼如下:

    public Node flatten(Node head) {
        if(head == null) return null;
        
        Node tmp = head;
        while(tmp != null) {
            if(tmp.child != null) {
                
                Node child = tmp.child;
                tmp.child = null;
                
                Node next = tmp.next;
                tmp.next = child;
                child.prev = tmp;
                while(child.next != null) {
                    child =  child.next;
                }
                
                if(next != null) {
                    child.next = next;
                    next.prev = child;
                }
            }
            tmp = tmp.next;
        }
        return head;
    }
思路3:減少遍歷次數(shù)

之前的兩種思路,都會(huì)出現(xiàn)大量的重復(fù)遍歷,重復(fù)遍歷和葉子節(jié)點(diǎn)的深度成正相關(guān),可以想方法將重復(fù)遍歷的次數(shù)減少。其實(shí),我們可以看見,無論我們何時(shí)將子節(jié)點(diǎn)展開,并拼接回父節(jié)點(diǎn)的雙鏈表中,子節(jié)點(diǎn)展開的雙鏈表的頭結(jié)點(diǎn)是固定的,并且可以用父節(jié)點(diǎn)訪問到。而尾節(jié)點(diǎn)必須通過重復(fù)遍歷來查找并拼接。因此,如果每次都將展開后的尾節(jié)點(diǎn)返回,就可以無需重復(fù)遍歷將展開的子節(jié)點(diǎn)拼接回父節(jié)點(diǎn)。代碼如下:

    public Node flatten(Node head) {
        flattenAndReturnTail(head);
        return head;
    }
    
    public Node flattenAndReturnTail(Node head) {
        if(head == null) return null;
        if(head.child == null) {
            if(head.next == null) return head;
            return flattenAndReturnTail(head.next);
        }else {
            Node child = head.child;
            head.child = null;
            
            Node next = head.next;
            Node childTail = flatten(child);
            head.next  = child;
            child.prev = head;
            if(next != null) {
                childTail.next = next;
                next.prev = childTail;
                return flattenAndReturnTail(next);
            }
            return childTail;
        }
    }

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/75192.html

相關(guān)文章

  • [LeetCode] 430. Flatten a Multilevel Doubly Linked

    摘要: Problem You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These...

    curried 評(píng)論0 收藏0
  • LeetCode 430:扁平化多級(jí)雙向鏈表 Flatten a Multilevel Doubly

    摘要:您將獲得一個(gè)雙向鏈表,除了下一個(gè)和前一個(gè)指針之外,它還有一個(gè)子指針,可能指向單獨(dú)的雙向鏈表。扁平化列表,使所有結(jié)點(diǎn)出現(xiàn)在單級(jí)雙鏈表中。 您將獲得一個(gè)雙向鏈表,除了下一個(gè)和前一個(gè)指針之外,它還有一個(gè)子指針,可能指向單獨(dú)的雙向鏈表。這些子列表可能有一個(gè)或多個(gè)自己的子項(xiàng),依此類推,生成多級(jí)數(shù)據(jù)結(jié)構(gòu),如下面的示例所示。 扁平化列表,使所有結(jié)點(diǎn)出現(xiàn)在單級(jí)雙鏈表中。您將獲得列表第一級(jí)的頭部。 Yo...

    sugarmo 評(píng)論0 收藏0
  • LeetCode 430:扁平化多級(jí)雙向鏈表 Flatten a Multilevel Doubly

    摘要:您將獲得一個(gè)雙向鏈表,除了下一個(gè)和前一個(gè)指針之外,它還有一個(gè)子指針,可能指向單獨(dú)的雙向鏈表。扁平化列表,使所有結(jié)點(diǎn)出現(xiàn)在單級(jí)雙鏈表中。 您將獲得一個(gè)雙向鏈表,除了下一個(gè)和前一個(gè)指針之外,它還有一個(gè)子指針,可能指向單獨(dú)的雙向鏈表。這些子列表可能有一個(gè)或多個(gè)自己的子項(xiàng),依此類推,生成多級(jí)數(shù)據(jù)結(jié)構(gòu),如下面的示例所示。 扁平化列表,使所有結(jié)點(diǎn)出現(xiàn)在單級(jí)雙鏈表中。您將獲得列表第一級(jí)的頭部。 Yo...

    dabai 評(píng)論0 收藏0
  • [LeetCode] 426. Convert BST to Sorted Doubly Linke

    Problem Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Lets take the foll...

    MartinDai 評(píng)論0 收藏0
  • [LintCode/LeetCode] Flatten Binary Tree to Linked

    Problem Flatten a binary tree to a fake linked list in pre-order traversal.Here we use the right pointer in TreeNode as the next pointer in ListNode. Example 1 1 ...

    TNFE 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<