题解 | #二叉树中和为某一值的路径(三)# Go语言
二叉树中和为某一值的路径(三)
https://www.nowcoder.com/practice/965fef32cae14a17a8e86c76ffe3131f?tpId=13&tqId=2277604&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13
package main
import . "nc_tools"
/*
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param sum int整型
* @return int整型
*/
var res = 0
func dfs(root *TreeNode, sum int) {
if root == nil {
return
}
if sum == root.Val {
res++
}
dfs(root.Left, sum-root.Val)
dfs(root.Right, sum-root.Val)
}
func FindPath( root *TreeNode , sum int ) int {
// write code here
if root == nil {
return res
}
dfs(root, sum)
FindPath(root.Left, sum)
FindPath(root.Right, sum)
return res
}