二叉树转换链表

前端之家收集整理的这篇文章主要介绍了二叉树转换链表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

二叉树转换链表

treeRoot =
new BioTree(1,new BioTree(2,new BioTree(3),new BioTree(4)
),new BioTree(5,new BioTree(6)
)
);

function recur(node,handler) {
if (node.left) {
recur(node.left,handler);
}
handler(node);
if (node.right) {
recur(node.right,handler);
}
}

let head = null;
let pre = null;
recur(treeRoot,v => {
if (pre === null) {
pre = v;
head = v;
} else {
pre.right = v;
pre = pre.right;
}
});

console.log('=================');

let cur = head
while(cur) {
console.log(cur.data);
cur = cur.right;
}

猜你在找的程序笔记相关文章