题解 | #二叉树中和为某一值的路径(三)#
二叉树中和为某一值的路径(三)
https://www.nowcoder.com/practice/965fef32cae14a17a8e86c76ffe3131f
public class Solution { int path; public int FindPath(TreeNode root, int sum) { if (root==null){ return path; } recursion(root,sum,0); FindPath(root.left,sum); FindPath(root.right,sum); return path; } public void recursion(TreeNode node, int sum, int pathSum) { if (node == null) return; pathSum += node.val; if (pathSum == sum) { path++; } recursion(node.left, sum, pathSum); recursion(node.right, sum, pathSum); } }