Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Created December 18, 2020 11:31
Show Gist options
  • Save vamsitallapudi/bd28ee2f9e280c89fab06c49362c0185 to your computer and use it in GitHub Desktop.
Save vamsitallapudi/bd28ee2f9e280c89fab06c49362c0185 to your computer and use it in GitHub Desktop.
fun preorderTraversal(root: TreeNode?): List<Int> {
// if root is null, return list of empty array
if (root == null) {
return listOf()
}
// iterate recursively into left child and right child
return listOf(root.data) + preorderTraversal(root.left) + preorderTraversal(root.right)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment