All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0897_increasing_order_search_tree.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
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)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy