This commit is contained in:
krahets
2023-02-08 16:47:52 +08:00
parent 139e34bdb1
commit 7156a5f832
15 changed files with 367 additions and 208 deletions
+10 -9
View File
@@ -120,12 +120,13 @@ comments: true
// 初始化一个列表,用于保存遍历序列
let list = [];
while (queue.length) {
let node = queue.shift(); // 队列出队
list.push(node.val); // 保存结点值
let node = queue.shift(); // 队列出队
list.push(node.val); // 保存结点值
if (node.left)
queue.push(node.left); // 左子结点入队
queue.push(node.left); // 左子结点入队
if (node.right)
queue.push(node.right); // 右子结点入队
queue.push(node.right); // 右子结点入队
}
return list;
}
@@ -368,14 +369,14 @@ comments: true
```javascript title="binary_tree_dfs.js"
/* 前序遍历 */
function preOrder(root){
function preOrder(root) {
if (root === null) return;
// 访问优先级:根结点 -> 左子树 -> 右子树
list.push(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* 中序遍历 */
function inOrder(root) {
if (root === null) return;
@@ -384,7 +385,7 @@ comments: true
list.push(root.val);
inOrder(root.right);
}
/* 后序遍历 */
function postOrder(root) {
if (root === null) return;
@@ -408,7 +409,7 @@ comments: true
preOrder(root.left);
preOrder(root.right);
}
/* 中序遍历 */
function inOrder(root: TreeNode | null): void {
if (root === null) {
@@ -419,7 +420,7 @@ comments: true
list.push(root.val);
inOrder(root.right);
}
/* 后序遍历 */
function postOrder(root: TreeNode | null): void {
if (root === null) {