g0001_0100.s0094_binary_tree_inorder_traversal.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0094_binary_tree_inorder_traversal
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
// #Stack #Data_Structure_I_Day_10_Tree #Udemy_Tree_Stack_Queue
// #2022_09_27_Time_269_ms_(38.80%)_Space_35.9_MB_(14.49%)
import com_github_leetcode.TreeNode
/*
* 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?): List {
if (root == null) {
return emptyList()
}
val answer: MutableList = ArrayList()
inorderTraversal(root, answer)
return answer
}
private fun inorderTraversal(root: TreeNode?, answer: MutableList) {
if (root == null) {
return
}
if (root.left != null) {
inorderTraversal(root.left, answer)
}
answer.add(root.`val`)
if (root.right != null) {
inorderTraversal(root.right, answer)
}
}
}