mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 23:56:29 +08:00
Fix the indentation of JS and TS code.
This commit is contained in:
parent
541f384e7c
commit
649d8c4c86
8 changed files with 169 additions and 149 deletions
|
@ -22,12 +22,12 @@ function preOrder(root, res) {
|
|||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树");
|
||||
console.log('\n初始化二叉树');
|
||||
printTree(root);
|
||||
|
||||
// 前序遍历
|
||||
const res = [];
|
||||
preOrder(root, res);
|
||||
|
||||
console.log("\n输出所有值为 7 的节点");
|
||||
console.log(res.map(node => node.val));
|
||||
console.log('\n输出所有值为 7 的节点');
|
||||
console.log(res.map((node) => node.val));
|
||||
|
|
|
@ -26,7 +26,7 @@ function preOrder(root, path, res) {
|
|||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树");
|
||||
console.log('\n初始化二叉树');
|
||||
printTree(root);
|
||||
|
||||
// 前序遍历
|
||||
|
@ -34,7 +34,7 @@ const path = [];
|
|||
const res = [];
|
||||
preOrder(root, path, res);
|
||||
|
||||
console.log("\n输出所有根节点到节点 7 的路径");
|
||||
res.forEach(path => {
|
||||
console.log(path.map(node => node.val));
|
||||
console.log('\n输出所有根节点到节点 7 的路径');
|
||||
res.forEach((path) => {
|
||||
console.log(path.map((node) => node.val));
|
||||
});
|
||||
|
|
|
@ -27,7 +27,7 @@ function preOrder(root, path, res) {
|
|||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树");
|
||||
console.log('\n初始化二叉树');
|
||||
printTree(root);
|
||||
|
||||
// 前序遍历
|
||||
|
@ -35,7 +35,7 @@ const path = [];
|
|||
const res = [];
|
||||
preOrder(root, path, res);
|
||||
|
||||
console.log("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点");
|
||||
res.forEach(path => {
|
||||
console.log(path.map(node => node.val));
|
||||
console.log('\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点');
|
||||
res.forEach((path) => {
|
||||
console.log(path.map((node) => node.val));
|
||||
});
|
||||
|
|
|
@ -56,14 +56,14 @@ function backtrack(state, choices, res) {
|
|||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树");
|
||||
console.log('\n初始化二叉树');
|
||||
printTree(root);
|
||||
|
||||
// 回溯算法
|
||||
const res = [];
|
||||
backtrack([], [root], res);
|
||||
|
||||
console.log("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
|
||||
res.forEach(path => {
|
||||
console.log(path.map(node => node.val));
|
||||
console.log('\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点');
|
||||
res.forEach((path) => {
|
||||
console.log(path.map((node) => node.val));
|
||||
});
|
||||
|
|
|
@ -23,12 +23,14 @@ function preOrder(root: TreeNode | null, res: TreeNode[]): void {
|
|||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树");
|
||||
console.log('\n初始化二叉树');
|
||||
printTree(root);
|
||||
|
||||
// 前序遍历
|
||||
const res: TreeNode[] = [];
|
||||
preOrder(root, res);
|
||||
|
||||
console.log("\n输出所有值为 7 的节点");
|
||||
console.log(res.map(node => node.val));
|
||||
console.log('\n输出所有值为 7 的节点');
|
||||
console.log(res.map((node) => node.val));
|
||||
|
||||
export {};
|
||||
|
|
|
@ -9,7 +9,11 @@ import { arrToTree } from '../modules/TreeNode';
|
|||
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) {
|
||||
return;
|
||||
}
|
||||
|
@ -27,7 +31,7 @@ function preOrder(root: TreeNode | null, path: TreeNode[], res: TreeNode[][]): v
|
|||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树");
|
||||
console.log('\n初始化二叉树');
|
||||
printTree(root);
|
||||
|
||||
// 前序遍历
|
||||
|
@ -35,7 +39,9 @@ const path: TreeNode[] = [];
|
|||
const res: TreeNode[][] = [];
|
||||
preOrder(root, path, res);
|
||||
|
||||
console.log("\n输出所有根节点到节点 7 的路径");
|
||||
res.forEach(path => {
|
||||
console.log(path.map(node => node.val));
|
||||
console.log('\n输出所有根节点到节点 7 的路径');
|
||||
res.forEach((path) => {
|
||||
console.log(path.map((node) => node.val));
|
||||
});
|
||||
|
||||
export {};
|
||||
|
|
|
@ -9,7 +9,11 @@ import { arrToTree } from '../modules/TreeNode';
|
|||
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) {
|
||||
return;
|
||||
|
@ -28,7 +32,7 @@ function preOrder(root: TreeNode | null, path: TreeNode[], res: TreeNode[][]): v
|
|||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树");
|
||||
console.log('\n初始化二叉树');
|
||||
printTree(root);
|
||||
|
||||
// 前序遍历
|
||||
|
@ -36,7 +40,9 @@ const path: TreeNode[] = [];
|
|||
const res: TreeNode[][] = [];
|
||||
preOrder(root, path, res);
|
||||
|
||||
console.log("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点");
|
||||
res.forEach(path => {
|
||||
console.log(path.map(node => node.val));
|
||||
console.log('\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点');
|
||||
res.forEach((path) => {
|
||||
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)) {
|
||||
// 记录解
|
||||
|
@ -57,14 +61,16 @@ function backtrack(state: TreeNode[], choices: TreeNode[], res: TreeNode[][]): v
|
|||
|
||||
// Driver Code
|
||||
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树");
|
||||
console.log('\n初始化二叉树');
|
||||
printTree(root);
|
||||
|
||||
// 回溯算法
|
||||
const res: TreeNode[][] = [];
|
||||
backtrack([], [root], res);
|
||||
|
||||
console.log("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
|
||||
res.forEach(path => {
|
||||
console.log(path.map(node => node.val));
|
||||
console.log('\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点');
|
||||
res.forEach((path) => {
|
||||
console.log(path.map((node) => node.val));
|
||||
});
|
||||
|
||||
export {};
|
||||
|
|
Loading…
Reference in a new issue