题解 | #二叉树中和为某一值的路径(一)#
二叉树中和为某一值的路径(一)
https://www.nowcoder.com/practice/508378c0823c423baa723ce448cbfd0c
class Solution { public: bool hasPathSum(TreeNode* root, int sum) { // write code here if(!root){//节点不存在返回0 return 0; }else{ sum = sum - root->val; if(sum == 0 && !root->left && !root->right){//当前n个节点和为n时,判断该节点是不是叶子节点 return true; } return hasPathSum(root->left,sum) || hasPathSum(root->right,sum); //递归 } } };