首页 > 解决方案 > 错误:需要属性名称或接收器类型 - Kotlin

问题描述

无法弄清楚下面的代码有什么问题以及为什么我得到以下错误:

/**
 * 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 pathSum(root: TreeNode?, sum: Int): List<List<Int>> {
        var result : List<MutableList<Int>> = listOf(mutableListOf())
        var path : MutableList<Int> = mutableListOf()
        dfs(root, sum, result, path)
        return result
    }

    fun dfs(root: TreeNode?, sum: Int, result: List<MutableList<Int>>, path: MutableList<Int>){

        if(root == null) return

        path.add(sum)

        dfs(root.left, sum - root.val, result, path)
        dfs(root.right, sum - root.val, result, path)

        if(sum == 0 &&
                root.left == null &&
                root.right == null) {           
            result.add(path)
        }

        path.remove(path.size() - 1)

    }
}

在运行上面的代码时,我遇到了很多编译时错误,这是 Kotlin 的新手,努力找出根本原因:

Line 24: Char 38: error: expecting property name or receiver type
            dfs(root.left, sum - root.val, result, path)
                                         ^
    Line 24: Char 46: error: expecting an element
            dfs(root.left, sum - root.val, result, path)

标签: kotlin

解决方案


推荐阅读