mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 20:26:29 +08:00
4325974af1
* feat: Add Dart codes for chapter_backtracking * feat: Add Dart codes for chapter_divide_and_conquer * Format Dart Code
35 lines
746 B
Dart
35 lines
746 B
Dart
/**
|
|
* 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));
|
|
}
|