https://leetcode.com/problems/binary-tree-inorder-traversal/description/ Easy

Решение

class Solution {
    fun inorderTraversal(root: TreeNode?): List<Int> {
        val res = ArrayList<Int>()
        var cur = root
        while (cur != null) {
            val left = cur.left
            if (left == null) {
                res.add(cur.`val`)
                cur = cur.right
            } else {
                var pre = left
                while (pre!!.right != null && pre.right !== cur) pre = pre.right
                if (pre.right == null) {
                    pre.right = cur
                    cur = cur.left
                } else {
                    pre.right = null
                    res.add(cur.`val`)
                    cur = cur.right
                }
            }
        }
        return res
    }
}