binary-tree-dom
前序、中序、后序二叉树算法
利用dom来演示,点击查看效果: demo
核心算法
// 前序 根左右
function preorder(el) {
if (!el) {
return;
}
divList.push(el);
const [leftChild, rightChild] = el.children;
preorder(leftChild);
preorder(rightChild);
}
// 左根右
function inorder(el) {
if (!el) {
return;
}
const [leftChild, rightChild] = el.children;
inorder(leftChild);
divList.push(el);
inorder(rightChild);
}
// 左右根
function postorder(el) {
if (!el) {
return;
}
const [leftChild, rightChild] = el.children;
postorder(leftChild);
postorder(rightChild);
divList.push(el);
}
Tags: