摘要:步驟如下代碼如下思路二循環(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
摘要: 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...
摘要:您將獲得一個(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...
摘要:您將獲得一個(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...
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...
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 ...
閱讀 1826·2021-10-09 09:44
閱讀 2699·2021-09-22 15:38
閱讀 2496·2021-09-09 09:33
閱讀 696·2021-09-07 09:58
閱讀 1827·2021-09-02 15:41
閱讀 2510·2019-08-30 15:55
閱讀 1801·2019-08-30 15:55
閱讀 543·2019-08-30 15:44