深度和广度优先
深度优先:
1.访问根结点
2.对根节点的children挨个进行深度优先遍历
// 深度优先遍历
const dfs = root => {
root.children.forEach(child => {
console.log(root.val)
dfs(child)
})
}
广度优先
1.新建一个队列,把根结点入队
2.把队头出队并访问
3.把队头的children挨个入队
4.重复地二三步,直到队列为空
// 广度优先遍历
const bfs = root => {
const q = [root];
while(q.length>0){
const n = q.shift();
console.log(n.val)
n.children.forEach(child => {
q.push(child)
})
}
}
二叉树:
定义:树中每个节点最多只能有两个字节点
在js中用object模拟二叉树
const binaryTree = {
val: 1,
left: {
val: 2,
left: null,
right: null
},
right: {
val: 3,
left: null,
right: null
}
}
先序遍历:
1.访问根结点
2.对根结点的左子树进行先序遍历
3.对根结点的右子树进行先序遍历
// 二叉树 先序遍历
const preOrder = root => {
if(!root) return;
console.log(root.val) // 访问根结点
preOrder(root.left)
preOrder(root.right)
}
中序遍历:
1.对根结点的左子树进行先序遍历
2.访问根结点
3.对根结点的右子树进行先序遍历
// 二叉树 中序遍历
const inOrder = root => {
if(!root) return;
inOrder(root.left)
console.log(root.val) // 访问根结点
inOrder(root.right)
}
后序遍历:
1.对根结点的左子树进行先序遍历
2.对根结点的右子树进行先序遍历
3.访问根结点
// 二叉树 后序遍历
const inOrder = root => {
if(!root) return;
inOrder(root.left)
inOrder(root.right)
console.log(root.val) // 访问根结点
}
二叉树先中后序遍历(非递归版)
先序遍历
const preOrder = root => {
if(!root) return;
const stack = [root];
const n = stack.pop();
console.log(n.val)// 根节点
while(stack.length){
if(n.right){
stach.push(n.right)
}
if(n.left){
stach.push(n.left)
}
}
}
中序遍历
const inOrder = root => {
if(!root) return;
const stack = [];
const p = root;
while(stack.length || p){
while(p){
stack.push(p)
p = p.left;
}
const n = stack.pop();
console.log(n.val) // 左节点第一个
p = n.right;
}
}
后序遍历
const preOrder = root => {
if(!root) return;
const stack = [root];
const outputStack = [];
while(stack.length){
const n = stack.pop();
outputStack.push(n)
if(n.left){
stach.push(n.left)
}
if(n.right){
stach.push(n.right)
}
}
while(outputStack.length){
const n = outputStack.pop();
console.log(n.val)
}
}