const root = {
val: 1,
left: {
val: 2,
left: { val: 4, left: null, right: null },
},
right: {
val: 3,
left: { val: 5, left: null, right: null },
right: { val: 6, left: null, right: null },
},
};
let res = [];
let res2 = [];
let res3 = [];
//前序遍历
const startTraversal = (root) => {
root.val && res.push(root.val);
root.left && startTraversal(root.left);
root.right && startTraversal(root.right);
return res;
};
//中序遍历
const midTraversal = (root) => {
root.left && midTraversal(root.left);
root.val && res2.push(root.val);
root.right && midTraversal(root.right);
return res2;
};
//后序遍历
const endTraversal = (root) => {
root.left && endTraversal(root.left);
root.right && endTraversal(root.right);
root.val && res3.push(root.val);
return res3;
};
//层序遍历
const levelTraversal = (root) => {
let res = [];
if (!root) {
return res;
}
let tree = [];
tree.push(root);
while (tree.length) {
let node = tree.shift();
if (node.val) {
res.push(node.val);
}
if (node.left) {
tree.push(node.left);
}
if (node.right) {
tree.push(node.right);
}
}
return res;
};
console.log("前序遍历:", startTraversal(root));
console.log("中序遍历:", midTraversal(root));
console.log("后序遍历:", endTraversal(root));
console.log("层序遍历:", levelTraversal(root));