You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
739 B
34 lines
739 B
/**
|
|
* File: preorder_traversal_i_compact.js
|
|
* Created Time: 2023-05-09
|
|
* Author: Justin (xiefahit@gmail.com)
|
|
*/
|
|
|
|
const { arrToTree } = require('../modules/TreeNode');
|
|
const { printTree } = require('../modules/PrintUtil');
|
|
|
|
/* 前序遍历:例题一 */
|
|
function preOrder(root, res) {
|
|
if (root === null) {
|
|
return;
|
|
}
|
|
if (root.val === 7) {
|
|
// 记录解
|
|
res.push(root);
|
|
}
|
|
preOrder(root.left, res);
|
|
preOrder(root.right, res);
|
|
}
|
|
|
|
// Driver Code
|
|
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
|
console.log('\n初始化二叉树');
|
|
printTree(root);
|
|
|
|
// 前序遍历
|
|
const res = [];
|
|
preOrder(root, res);
|
|
|
|
console.log('\n输出所有值为 7 的节点');
|
|
console.log(res.map((node) => node.val));
|