Fix the indentation of JS and TS code.

pull/495/head
krahets 2 years ago
parent 541f384e7c
commit 649d8c4c86

@ -22,12 +22,12 @@ function preOrder(root, res) {
// Driver Code // Driver Code
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]); const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树"); console.log('\n初始化二叉树');
printTree(root); printTree(root);
// 前序遍历 // 前序遍历
const res = []; const res = [];
preOrder(root, res); preOrder(root, res);
console.log("\n输出所有值为 7 的节点"); console.log('\n输出所有值为 7 的节点');
console.log(res.map(node => node.val)); console.log(res.map((node) => node.val));

@ -26,7 +26,7 @@ function preOrder(root, path, res) {
// Driver Code // Driver Code
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]); const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树"); console.log('\n初始化二叉树');
printTree(root); printTree(root);
// 前序遍历 // 前序遍历
@ -34,7 +34,7 @@ const path = [];
const res = []; const res = [];
preOrder(root, path, res); preOrder(root, path, res);
console.log("\n输出所有根节点到节点 7 的路径"); console.log('\n输出所有根节点到节点 7 的路径');
res.forEach(path => { res.forEach((path) => {
console.log(path.map(node => node.val)); console.log(path.map((node) => node.val));
}); });

@ -27,7 +27,7 @@ function preOrder(root, path, res) {
// Driver Code // Driver Code
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]); const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树"); console.log('\n初始化二叉树');
printTree(root); printTree(root);
// 前序遍历 // 前序遍历
@ -35,7 +35,7 @@ const path = [];
const res = []; const res = [];
preOrder(root, path, res); preOrder(root, path, res);
console.log("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点"); console.log('\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点');
res.forEach(path => { res.forEach((path) => {
console.log(path.map(node => node.val)); console.log(path.map((node) => node.val));
}); });

@ -56,14 +56,14 @@ function backtrack(state, choices, res) {
// Driver Code // Driver Code
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]); const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树"); console.log('\n初始化二叉树');
printTree(root); printTree(root);
// 回溯算法 // 回溯算法
const res = []; const res = [];
backtrack([], [root], res); backtrack([], [root], res);
console.log("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点"); console.log('\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点');
res.forEach(path => { res.forEach((path) => {
console.log(path.map(node => node.val)); console.log(path.map((node) => node.val));
}); });

@ -23,12 +23,14 @@ function preOrder(root: TreeNode | null, res: TreeNode[]): void {
// Driver Code // Driver Code
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]); const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树"); console.log('\n初始化二叉树');
printTree(root); printTree(root);
// 前序遍历 // 前序遍历
const res: TreeNode[] = []; const res: TreeNode[] = [];
preOrder(root, res); preOrder(root, res);
console.log("\n输出所有值为 7 的节点"); console.log('\n输出所有值为 7 的节点');
console.log(res.map(node => node.val)); console.log(res.map((node) => node.val));
export {};

@ -9,7 +9,11 @@ import { arrToTree } from '../modules/TreeNode';
import { printTree } from '../modules/PrintUtil'; import { printTree } from '../modules/PrintUtil';
/* 前序遍历:例题二 */ /* 前序遍历:例题二 */
function preOrder(root: TreeNode | null, path: TreeNode[], res: TreeNode[][]): void { function preOrder(
root: TreeNode | null,
path: TreeNode[],
res: TreeNode[][]
): void {
if (root === null) { if (root === null) {
return; return;
} }
@ -27,7 +31,7 @@ function preOrder(root: TreeNode | null, path: TreeNode[], res: TreeNode[][]): v
// Driver Code // Driver Code
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]); const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树"); console.log('\n初始化二叉树');
printTree(root); printTree(root);
// 前序遍历 // 前序遍历
@ -35,7 +39,9 @@ const path: TreeNode[] = [];
const res: TreeNode[][] = []; const res: TreeNode[][] = [];
preOrder(root, path, res); preOrder(root, path, res);
console.log("\n输出所有根节点到节点 7 的路径"); console.log('\n输出所有根节点到节点 7 的路径');
res.forEach(path => { res.forEach((path) => {
console.log(path.map(node => node.val)); console.log(path.map((node) => node.val));
}); });
export {};

@ -9,7 +9,11 @@ import { arrToTree } from '../modules/TreeNode';
import { printTree } from '../modules/PrintUtil'; import { printTree } from '../modules/PrintUtil';
/* 前序遍历:例题三 */ /* 前序遍历:例题三 */
function preOrder(root: TreeNode | null, path: TreeNode[], res: TreeNode[][]): void { function preOrder(
root: TreeNode | null,
path: TreeNode[],
res: TreeNode[][]
): void {
// 剪枝 // 剪枝
if (root === null || root.val === 3) { if (root === null || root.val === 3) {
return; return;
@ -28,7 +32,7 @@ function preOrder(root: TreeNode | null, path: TreeNode[], res: TreeNode[][]): v
// Driver Code // Driver Code
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]); const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树"); console.log('\n初始化二叉树');
printTree(root); printTree(root);
// 前序遍历 // 前序遍历
@ -36,7 +40,9 @@ const path: TreeNode[] = [];
const res: TreeNode[][] = []; const res: TreeNode[][] = [];
preOrder(root, path, res); preOrder(root, path, res);
console.log("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点"); console.log('\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点');
res.forEach(path => { res.forEach((path) => {
console.log(path.map(node => node.val)); console.log(path.map((node) => node.val));
}); });
export {};

@ -34,7 +34,11 @@ function undoChoice(state: TreeNode[]): void {
} }
/* 回溯算法:例题三 */ /* 回溯算法:例题三 */
function backtrack(state: TreeNode[], choices: TreeNode[], res: TreeNode[][]): void { function backtrack(
state: TreeNode[],
choices: TreeNode[],
res: TreeNode[][]
): void {
// 检查是否为解 // 检查是否为解
if (isSolution(state)) { if (isSolution(state)) {
// 记录解 // 记录解
@ -57,14 +61,16 @@ function backtrack(state: TreeNode[], choices: TreeNode[], res: TreeNode[][]): v
// Driver Code // Driver Code
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]); const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
console.log("\n初始化二叉树"); console.log('\n初始化二叉树');
printTree(root); printTree(root);
// 回溯算法 // 回溯算法
const res: TreeNode[][] = []; const res: TreeNode[][] = [];
backtrack([], [root], res); backtrack([], [root], res);
console.log("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点"); console.log('\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点');
res.forEach(path => { res.forEach((path) => {
console.log(path.map(node => node.val)); console.log(path.map((node) => node.val));
}); });
export {};

Loading…
Cancel
Save