mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-17 20:17:14 +00: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,35 @@
|
||||
/**
|
||||
* File: preorder_traversal_i_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> res) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
if (root.val == 7) {
|
||||
// 记录解
|
||||
res.add(root);
|
||||
}
|
||||
preOrder(root.left, res);
|
||||
preOrder(root.right, res);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
TreeNode? root = listToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
print("\n初始化二叉树");
|
||||
printTree(root);
|
||||
|
||||
// 前序遍历
|
||||
List<TreeNode> res = [];
|
||||
preOrder(root, res);
|
||||
|
||||
print("\n输出所有值为 7 的节点");
|
||||
print(List.generate(res.length, (i) => res[i].val));
|
||||
}
|
||||
Reference in New Issue
Block a user