Created
December 9, 2022 17:36
-
-
Save gabrielepalma/0771ae048cf7f0c6f2f5d90c1a73aa74 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
class Solution { | |
func inorderTraversal(_ root: TreeNode?) -> [Int] { | |
var stack: [(TreeNode, Bool)] = (root != nil) ? [(root!, true)] : [] | |
var output:[Int] = [] | |
while let (node, unvisited) = stack.popLast() { | |
guard unvisited else { | |
output.append(node.val) | |
print(node.val) | |
continue | |
} | |
if let right = node.right { | |
stack.append((right, true)) | |
} | |
stack.append((node, false)) | |
if let left = node.left { | |
stack.append((left, true)) | |
} | |
} | |
return output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment