using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param sum int整型 * @return bool布尔型 */ bool res = false; public bool hasPathSum (TreeNode root, int sum) { PathGet(root, sum); return res; } public void PathGet(TreeNode root, int target){ if(root == null || res) return; target -= root.val; if(target == 0 && root.left == null && root.right == null){ res = true; return; } PathGet(root.left, target); PathGet(root.right, target); } }