|
|
|
@ -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 {};
|
|
|
|
|