mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-22 14:26:34 +08:00
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
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user