TOP101题解|BM29#二叉树中和为某一值的路径(一)#
二叉树中和为某一值的路径(一)
https://www.nowcoder.com/practice/508378c0823c423baa723ce448cbfd0c
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * @author Senky * @date 2023.07.28 * @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1 * @brief 加上了一个值而已,和算树的深度本质上没什么不同 * @param root TreeNode类 * @param sum int整型 * @return bool布尔型 */ #include <stdbool.h> bool hasPathSum(struct TreeNode* root, int sum ) { // write code here if(root == NULL) { return false; } else if( sum == root->val && NULL == root->left && NULL == root->right) { return true; } bool left = hasPathSum(root->left, sum - root->val); bool right = hasPathSum(root->right, sum - root->val); return (left || right); }#TOP101#
TOP101-BM系列 文章被收录于专栏
系列的题解