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.
48 lines
967 B
48 lines
967 B
/**
|
|
* File: preorder_traversal_ii_compact.dart
|
|
* Created Time: 2023-08-10
|
|
* Author: liuyuxin (gvenusleo@gmail.com)
|
|
*/
|
|
|
|
import '../utils/print_util.dart';
|
|
import '../utils/tree_node.dart';
|
|
|
|
/* 前序遍历:例题二 */
|
|
void preOrder(
|
|
TreeNode? root,
|
|
List<TreeNode> path,
|
|
List<List<TreeNode>> res,
|
|
) {
|
|
if (root == null) {
|
|
return;
|
|
}
|
|
|
|
// 尝试
|
|
path.add(root);
|
|
if (root.val == 7) {
|
|
// 记录解
|
|
res.add(List.from(path));
|
|
}
|
|
preOrder(root.left, path, res);
|
|
preOrder(root.right, path, res);
|
|
// 回退
|
|
path.removeLast();
|
|
}
|
|
|
|
/* Driver Code */
|
|
void main() {
|
|
TreeNode? root = listToTree([1, 7, 3, 4, 5, 6, 7]);
|
|
print("\n初始化二叉树");
|
|
printTree(root);
|
|
|
|
// 前序遍历
|
|
List<TreeNode> path = [];
|
|
List<List<TreeNode>> res = [];
|
|
preOrder(root, path, res);
|
|
|
|
print("\n输出所有根节点到节点 7 的路径");
|
|
for (List<TreeNode> vals in res) {
|
|
print(List.generate(vals.length, (i) => vals[i].val));
|
|
}
|
|
}
|