Created
June 7, 2020 02:25
-
-
Save davidseek/5b831ab8d24cd8cc804e4d02c1155581 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Queue { | |
| typealias NodeAtLevel = (node: TreeNode, level: Int) | |
| private var enqueueStack: [NodeAtLevel] = [] | |
| private var dequeueStack: [NodeAtLevel] = [] | |
| public var isEmpty: Bool { | |
| return enqueueStack.isEmpty && dequeueStack.isEmpty | |
| } | |
| public mutating func enqueue(_ node: TreeNode, at level: Int) { | |
| enqueueStack.append((node, level)) | |
| } | |
| public mutating func dequeue() -> NodeAtLevel? { | |
| if dequeueStack.isEmpty { | |
| dequeueStack = enqueueStack.reversed() | |
| enqueueStack.removeAll() | |
| } | |
| return dequeueStack.popLast() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment