一、题目地址
https://leetcode-cn.com/problems/Qv1Da2/
二、具体代码
/**
* // Definition for a Node.
* function Node(val,prev,next,child) {
* this.val = val;
* this.prev = prev;
* this.next = next;
* this.child = child;
* };
*/
/**
* @param {Node} head
* @return {Node}
*/
function flattenLisT(head) {
let node = head;
let tail = null;
while(node) {
let next = node.next;
//子元素展开
if(node.child) {
let child = node.child;
let childTail = flattenLisT(child);
node.child = null;//需要把原本的child子元素删除,方便后面扁平化管理
node.next = child;
child.prev = node;
childTail.next = next;
if(next) {
next.prev = childTail;
}
tail = childTail;
}else {
tail = node;
}
node = next;
}
return tail;
}
var flatten = function(head) {
if(head === null) {
return null;
}
flattenLisT(head);
return head;
};
三、补充链接
https://leetcode-cn.com/problems/Qv1Da2/solution/jian-zhi-offer-zhuan-xiang-tu-po-ban-shu-d2k4/