# javascript进阶必备的二叉树知识

·  阅读 314

### 二叉树介绍

#### 二叉树树的遍历

``````// 这里用 js 对象的形式写一个简单的二叉树
var treenode = {
value: 1,
left: {
value: 2,
left: {
value: 4,
},
right: {
value: 5,
left: {
value: 7,
},
right: {
value: 8,
},
},
},
right: {
value: 3,
right: {
value: 6,
},
},
}

• 前序遍历

``````// 递归实现
function preOrderTraversal(root) {
var result = []
function preOrderTraversalNode(node) {
if (node) {
result.push(node.value)
if (node.left) {
preOrderTraversalNode(node.left)
}
if (node.right) {
preOrderTraversalNode(node.right)
}
}
}
preOrderTraversalNode(root)
return result
}

``````// 迭代实现
function preOrderTraversal(root) {
var list = [],
stack = []

if (root) {
stack.push(root)
}

while (stack.length > 0) {
var curNode = stack.pop()
list.push(curNode.value)
if (curNode.right) {
stack.push(curNode.right)
}
if (curNode.left) {
stack.push(curNode.left)
}
}
return list
}

``````preOrderTraversal(treenode) // [1, 2, 4, 5, 7, 8, 3, 6]

• 中序遍历

``````// 递归实现
function inorderTraversal(root) {
var result = []
function inOrderTraversalNode(node) {
if (node) {
if (node.left) {
inOrderTraversalNode(node.left)
}
result.push(node.value)
if (node.right) {
inOrderTraversalNode(node.right)
}
}
}
inOrderTraversalNode(root)
return result
}

``````// 迭代实现
function inorderTraversal(root) {
var list = [],
stack = []

while (root || stack.length > 0) {
while (root) {
stack.push(root);
root = root.left;
}
root = stack.pop();
list.push(root.value);
root = root.right;
}
return list;
}

``````inorderTraversal(treenode) // [4, 2, 7, 5, 8, 1, 3, 6]

• 后序遍历

``````// 递归实现
function postorderTraversal(root) {
var result = []
function postorderTraversalNode(node) {
if (node) {
if (node.left) {
postorderTraversalNode(node.left)
}
if (node.right) {
postorderTraversalNode(node.right)
}
result.push(node.value)
}
}
postorderTraversalNode(root)
return result
}

``````// 迭代实现
function postorderTraversal(root) {
var list = [],
stack = []

if (root) {
stack.push(root)
}

while (stack.length > 0) {
var curNode = stack.pop()
list.unshift(curNode.value)
if (curNode.left) {
stack.push(curNode.left)
}
if (curNode.right) {
stack.push(curNode.right)
}
}
return list
}

``````postorderTraversal(treenode) // [4, 7, 8, 5, 2, 6, 3, 1]

• 层次遍历

``````function levelorderTraversal(root) {
const list = [];
let stack = [];
if (root) {
stack.push(root);
}
while (stack.length > 0) {
const newStack = [];
for (let i = 0; i < stack.length; i++) {
const curNode = stack[i];
list.push(curNode.value);
if (curNode.left) {
newStack.push(curNode.left);
}
if (curNode.right) {
newStack.push(curNode.right);
}
}
stack = newStack;
}
return list;
}

``````levelorderTraversal(treenode) // [1, 2, 3, 4, 5, 6, 7, 8]

• 对称二叉树判断

``````const isSymmetrical = (treenodeA, treenodeB) => {
if (!treenodeA && !treenodeB) {
return true
}
if (!treenodeA || !treenodeB) {
return false
}
if (treenodeA.value !== treenodeB.value) {
return false
}
return isSymmetrical(treenodeA.left, treenodeB.right) && isSymmetrical(treenodeA.right, treenodeB.left)
}

``````const treenodeB = {
value: 1,
left: {
value: 3,
left: {
value: 6,
},
},
right: {
value: 2,
left: {
value: 5,
left: {
value: 8,
},
right: {
value: 7,
},
},
right: {
value: 4,
},
},
}

isSymmetrical(treenode, treenodeB) // true