1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/swift/binary-tree-inorder-traversal.swift

31 lines
933 B
Swift
Raw Normal View History

/**
* 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
}
}