problems(swift): add “94. Binary Tree Inorder Traversal”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
11187c2700
commit
fba591dc92
1 changed files with 30 additions and 0 deletions
30
problems/swift/binary-tree-inorder-traversal.swift
Normal file
30
problems/swift/binary-tree-inorder-traversal.swift
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue