Created
December 18, 2020 11:31
-
-
Save vamsitallapudi/bd28ee2f9e280c89fab06c49362c0185 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
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