递归。
#include "stdbool.h" #define NULL ((void *)0) //Definition for a binary tree node. struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; bool hasPathSum(struct TreeNode* root, int targetSum){ if(root==NULL) return false; else if (root->left == NULL && root->right == NULL) return targetSum == root->val; else return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val); }
原文:https://www.cnblogs.com/vicky2021/p/14827226.html