概念
二叉树是树中每个节点最多只能有两个子节点的树。
实现
在JavaScript中常用Object
来实现二叉树,如上图二叉树可表示为:
const bt = {
val: 1,
left: {
val: 2,
left: {
val: 4,
left: null,
right: null
},
right: {
val: 5,
left: {
val: 8,
left: null,
right: null
},
right: {
val: 9,
left: null,
right: null
}
}
},
right: {
val: 3,
left: {
val: 6,
left: null,
right: null
},
right: {
val: 7,
left: null,
right: null
}
}
};
遍历
先序遍历
- 访问根节点
- 对根节点的左子树进行先序遍历
- 对根节点的右子树进行先序遍历
递归版:
// 递归先序遍历: 根 - 左 - 右
const preorder = root => {
if (!root) {
return; }
console.log(root.val); // 根
preorder(root.left); // 左
preorder(root.right); // 右
};
非递归版:
// 递归先序遍历(非递归)
const preorder = root => {
if (!root) {
return; }
const stack = [root];
while (stack.length) {
// 栈顶元素
const n = stack.pop();
console.log(n.val);
if (n.right) stack.push(n.right);
if (n.left) stack.push(n.left);
}
};
中序遍历
- 对根节点的左子树进行先序遍历
- 访问根节点
- 对根节点的右子树进行先序遍历
递归版:
// 递归中序遍历:左-根-右
const inorder = root => {
if (!root) {
return; }
inorder(root.left); // 左
console.log(root.val); // 根
inorder(root.right); // 右
};
非递归版:
// 递归中序遍历(非递归)
const inorder = root => {
if (!root) {
return; }
const stack = [];
let 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 postorder = root => {
if (!root) {
return; }
postorder(root.left); // 左
postorder(root.right); // 右
console.log(root.val); // 根
};
非递归版:
// 递归后序遍历(非递归)
const postorder = root => {
if (!root) {
return; }
const outStack = [];
const stack = [root];
while (stack.length) {
const n = stack.pop();
outStack.push(n);
if (n.left) stack.push(n.left);
if (n.right) stack.push(n.right);
}
while (outStack.length) {
const n = outStack.pop();
console.log(n.val);
}
};