1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

problems(swift): add “94. Binary Tree Inorder Traversal”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-09 12:53:31 +01:00
parent 11187c2700
commit fba591dc92
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,30 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
func inorderTraversal(_ root: TreeNode?, _ values: inout [Int]) {
if let root = root {
inorderTraversal(root.left, &values)
values.append(root.val)
inorderTraversal(root.right, &values)
}
}
func inorderTraversal(_ root: TreeNode?) -> [Int] {
var traversal: [Int] = []
inorderTraversal(root, &traversal)
return traversal
}
}