hello-algo/codes/dart/chapter_backtracking/preorder_traversal_iii_compact.dart
liuyuxin 4325974af1
feat: Add Dart codes for chapter_backtracking and chapter_divide_and_conquer (#680)
* feat: Add Dart codes for chapter_backtracking

* feat: Add Dart codes for chapter_divide_and_conquer

* Format Dart Code
2023-08-10 23:48:56 +08:00

49 lines
1,020 B
Dart

/**
* File: preorder_traversal_iii_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 || root.val == 3) {
return;
}
// 尝试
path.add(root);
if (root.val == 7) {
// 记录解
res.add(List.from(path));
path.removeLast();
return;
}
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));
}
}