g0801_0900.s0897_increasing_order_search_tree.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 g0801_0900.s0897_increasing_order_search_tree
// #Easy #Depth_First_Search #Tree #Binary_Tree #Stack #Binary_Search_Tree
// #2023_04_12_Time_128_ms_(85.71%)_Space_34_MB_(14.29%)
import com_github_leetcode.TreeNode
import java.util.LinkedList
/*
* 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 increasingBST(root: TreeNode?): TreeNode {
val list: MutableList = LinkedList()
traverse(root, list)
for (i in 1 until list.size) {
list[i - 1].right = list[i]
list[i].left = null
}
return list[0]
}
private fun traverse(root: TreeNode?, list: MutableList) {
if (root != null) {
traverse(root.left, list)
list.add(root)
traverse(root.right, list)
}
}
}