1.前序遍历
先访问根节点->左子树->右子树
// 二叉树的前序遍历
function DLR_deep(tree){
console.log(tree.value);
if(tree.left){
DLR_deep(tree.left);
}
if(tree.right){
DLR_deep(tree.right);
}
}
2.中序遍历
先访问左子树->根节点->右子树
function LDR_deep(tree){
if(tree.left){
LDR_deep(tree.left);
}
console.log(tree.value);
if(tree.right){
LDR_deep(tree.right);
}
}
3.后续遍历
先访问左子树->右子树->根节点
// 二叉树的后序遍历
function LRD_deep(tree){
if(tree.left){
LRD_deep(tree.left);
}
if(tree.right){
LRD_deep(tree.right);
}
console.log(tree.value);
}