Skip to content

Instantly share code, notes, and snippets.

@codetalks-new
Created June 13, 2015 23:30
Show Gist options
  • Select an option

  • Save codetalks-new/43345ee2086de4d15658 to your computer and use it in GitHub Desktop.

Select an option

Save codetalks-new/43345ee2086de4d15658 to your computer and use it in GitHub Desktop.
func preorderTraversal(root:TreeNode) -> [Int]{
var arr = [Int]()
var nodes = [TreeNode]()
nodes.append(root)
while !nodes.isEmpty{
let current = removeLast(&nodes)
arr.append(current.val)
if let right = current.right{
nodes.append(right)
}
if let left = current.left{
nodes.append(left)
}
}
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment