/** * 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 path, List> res, ) { if (root == null || root.val == 3) { 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 path = []; List> res = []; preOrder(root, path, res); print("\n输出所有根节点到节点 7 的路径"); for (List vals in res) { print(List.generate(vals.length, (i) => vals[i].val)); } }