![JAR search and dependency download from the Maven repository](/logo.png)
g0101_0200.s0144_binary_tree_preorder_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 g0101_0200.s0144_binary_tree_preorder_traversal
// #Easy #Depth_First_Search #Tree #Binary_Tree #Stack #Data_Structure_I_Day_10_Tree
// #Udemy_Tree_Stack_Queue #2022_10_09_Time_277_ms_(37.90%)_Space_34.2_MB_(82.19%)
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 preorderTraversal(root: TreeNode?): List {
val result: MutableList = ArrayList()
if (root == null) {
return result
}
val stack: ArrayDeque = ArrayDeque()
var current: TreeNode? = root
while (current != null || stack.isNotEmpty()) {
while (current != null) {
result.add(current.`val`)
stack.addLast(current.right)
current = current.left
}
current = stack.removeLast()
}
return result
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy