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

g0401_0500.s0437_path_sum_iii.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.28
Show newest version
package g0401_0500.s0437_path_sum_iii

// #Medium #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree
// #Big_O_Time_O(n)_Space_O(n) #2022_09_11_Time_403_ms_(54.12%)_Space_41.7_MB_(62.35%)

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
 * }
 */
@Suppress("NAME_SHADOWING")
class Solution {
    private var count = 0

    fun pathSum(root: TreeNode?, targetSum: Int): Int {
        if (root == null) {
            return 0
        }
        helper(root, targetSum, 0)
        pathSum(root.left, targetSum)
        pathSum(root.right, targetSum)
        return count
    }

    fun helper(node: TreeNode, targetSum: Int, currSum: Long) {
        var currSum = currSum
        currSum += node.`val`
        if (targetSum.toLong() == currSum) {
            count++
        }
        if (node.left != null) {
            helper(node.left!!, targetSum, currSum)
        }
        if (node.right != null) {
            helper(node.right!!, targetSum, currSum)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy