Created
June 13, 2015 23:30
-
-
Save codetalks-new/43345ee2086de4d15658 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
| 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