feat: add Swift codes for backtracking_algorithm article (#480)

* fix: compile error

* fix: package define

* feat: add Swift codes for backtracking_algorithm article
This commit is contained in:
nuomi1
2023-05-03 18:45:43 +08:00
committed by GitHub
parent ca76336a55
commit 561ef20462
7 changed files with 241 additions and 6 deletions
@@ -0,0 +1,43 @@
/**
* File: preorder_traversal_i_compact.swift
* Created Time: 2023-04-30
* Author: nuomi1 (nuomi1@qq.com)
*/
import utils
var res: [TreeNode] = []
/* */
func preOrder(root: TreeNode?) {
guard let root = root else {
return
}
if root.val == 7 {
//
res.append(root)
}
preOrder(root: root.left)
preOrder(root: root.right)
}
@main
enum PreorderTraversalICompact {
/* Driver Code */
static func main() {
let root = TreeNode.listToTree(list: [1, 7, 3, 4, 5, 6, 7])
print("\n初始化二叉树")
PrintUtil.printTree(root: root)
//
res = []
preOrder(root: root)
print("\n输出所有值为 7 的节点")
var vals: [Int] = []
for node in res {
vals.append(node.val)
}
print(vals)
}
}