1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00
LeetCode/kt/binary-tree-inorder-traversal.kt
Matej Focko aaaebf1d52
style(kt): reformat the files
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-05-17 18:23:38 +02:00

28 lines
652 B
Kotlin

/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun inorderTraversal(
root: TreeNode?,
values: MutableList<Int>,
): List<Int> {
if (root == null) {
return values
}
inorderTraversal(root.left, values)
values.add(root.`val`!!)
inorderTraversal(root.right, values)
return values
}
fun inorderTraversal(root: TreeNode?): List<Int> = inorderTraversal(root, mutableListOf<Int>())
}