mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 21:16:28 +08:00
4325974af1
* feat: Add Dart codes for chapter_backtracking * feat: Add Dart codes for chapter_divide_and_conquer * Format Dart Code
47 lines
967 B
Dart
47 lines
967 B
Dart
/**
|
|
* 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));
|
|
}
|
|
}
|