mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
problems: add inorder bst traversal
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
5a9d67f37d
commit
3460ef6dbd
1 changed files with 26 additions and 0 deletions
26
problems/binary-tree-inorder-traversal.kt
Normal file
26
problems/binary-tree-inorder-traversal.kt
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/**
|
||||||
|
* 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>())
|
||||||
|
}
|
Loading…
Reference in a new issue