每种遍历都通过递归和迭代两种方法实现
前序遍历代码
function TreeNode(val, left, right) {
this.val = (val === undefined ? 0 : val);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
let root_1_4 = new TreeNode(4);
let root_1_5 = new TreeNode(5);
let root_1_6 = new TreeNode(6);
let root_1_7 = new TreeNode(7);
let root_1_2 = new TreeNode(2, root_1_4, root_1_5);
let root_1_3 = new TreeNode(3, root_1_6, root_1_7);
let root_1_1 = new TreeNode(1, root_1_2, root_1_3);
var preorderTraversal_1 = function (root) {
let result = [];
if (!root) {
return result;
}
const order = (root) => {
result.push(root.val);
if (root.left !== null) {
order(root.left);
}
if (root.right !== null) {
order(root.right);
}
}
order(root);
return result;
};
var preorderTraversal_2 = function (root) {
let result = [];
if (!root) {
return result;
}
let stack = [];
stack.push(root);
while (stack.length) {
let current = stack.pop();
result.push(current.val);
if (current.right) {
stack.push(current.right);
}
if (current.left) {
stack.push(current.left);
}
}
return result;
};
console.log("前序遍历");
console.log("正确结果: 1, 2, 4, 5, 3, 6, 7");
console.log("递归法:" + preorderTraversal_1(root_1_1));
console.log("迭代法:" + preorderTraversal_2(root_1_1));
中序遍历代码
function TreeNode(val, left, right) {
this.val = (val === undefined ? 0 : val);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
let root_2_4 = new TreeNode(4);
let root_2_5 = new TreeNode(5);
let root_2_6 = new TreeNode(6);
let root_2_7 = new TreeNode(7);
let root_2_2 = new TreeNode(2, root_2_4, root_2_5);
let root_2_3 = new TreeNode(3, root_2_6, root_2_7);
let root_2_1 = new TreeNode(1, root_2_2, root_2_3);
var inorderTraversal_1 = function (root) {
let result = [];
if (!root) {
return [];
}
const order = (root) => {
if (root.left) {
order(root.left);
}
result.push(root.val);
if (root.right) {
order(root.right);
}
}
order(root);
return result;
};
var inorderTraversal_2 = function (root) {
let result = [];
if (!root) {
return [];
}
let stack = [];
let temp = root;
while (temp) {
stack.push(temp);
temp = temp.left;
}
while (stack.length) {
let current = stack.pop();
result.push(current.val);
if (current.right) {
let temp2 = current.right;
while (temp2) {
stack.push(temp2);
temp2 = temp2.left;
}
}
}
return result;
};
console.log("中序遍历");
console.log("正确结果: 4, 2, 5, 1, 6, 3, 7");
console.log("递归法:" + inorderTraversal_1(root_2_1));
console.log("递归法:" + inorderTraversal_2(root_2_1));
后序遍历代码
function TreeNode(val, left, right) {
this.val = (val === undefined ? 0 : val);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
let root_3_4 = new TreeNode(4);
let root_3_5 = new TreeNode(5);
let root_3_6 = new TreeNode(6);
let root_3_7 = new TreeNode(7);
let root_3_2 = new TreeNode(2, root_3_4, root_3_5);
let root_3_3 = new TreeNode(3, root_3_6, root_3_7);
let root_3_1 = new TreeNode(1, root_3_2, root_3_3);
var postorderTraversal_1 = function (root) {
let result = [];
if (!root) {
return result;
}
const order = (root) => {
if (root.left) {
order(root.left);
}
if (root.right) {
order(root.right);
}
result.push(root.val);
}
order(root);
return result;
};
var postorderTraversal_2 = function (root) {
let result = [];
if (!root) {
return result;
}
let stack = [];
let temp = root;
while (temp) {
stack.push(temp);
temp = temp.left;
}
let current = null;
while (stack.length) {
let top = stack[stack.length - 1];
if (!top.right || (top.right && current === top.right)) {
current = stack.pop();
result.push(top.val);
} else {
let temp2 = top.right;
while (temp2) {
stack.push(temp2);
temp2 = temp2.left;
}
}
}
return result;
};
console.log("后序遍历");
console.log("正确结果: 4, 5, 2, 6, 7, 3, 1");
console.log("递归法:" + postorderTraversal_1(root_3_1));
console.log("递归法:" + postorderTraversal_2(root_3_1));
实现页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>前序中序后序</title>
</head>
<body>
</body>
</html>
<script src = "前序.js"></script>
<script src = "中序.js"></script>
<script src = "后序.js"></script>
最后结果展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/aff9d52dde2c32f11be7ac5c95eed5e0.png)