BM29. 二叉树中和为某一值的路径(一)
描述
示例1
输入:
{5,4,8,1,11,#,9,#,#,2,7},22
输出:
true
示例2
输入:
{1,2},0
输出:
false
示例3
输入:
{1,2},3
输出:
true
示例4
输入:
{},0
输出:
false
C++ 解法, 执行用时: 3ms, 内存消耗: 768KB, 提交时间: 2022-07-26
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @param sum int整型 * @return bool布尔型 */ bool dfs(TreeNode* node, int num, int target) { int n = num + node->val; bool l = false, r = false; if (node->left != nullptr) { l = true; } if (node->right != nullptr) { r = true; } if (!l && !r) { return n == target; } bool ret = false; if (l) { ret = ret | dfs(node->left, n, target); } if (r) { ret = ret | dfs(node->right, n, target); } return ret; } bool hasPathSum(TreeNode* root, int sum) { // write code here if (root == nullptr) { return false; } return dfs(root, 0, sum); } };
C++ 解法, 执行用时: 3ms, 内存消耗: 768KB, 提交时间: 2022-06-19
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @param sum int整型 * @return bool布尔型 */ bool hasPathSum(TreeNode* root, int sum) { // write code here if(root == NULL) { return false; } if(root->val == sum && root->left == NULL && root->right == NULL) { return true; } return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); } };
C++ 解法, 执行用时: 3ms, 内存消耗: 768KB, 提交时间: 2022-06-16
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @param sum int整型 * @return bool布尔型 */ int dfs(TreeNode* node, int pre,int res) { if(node==nullptr) return 0; int sum=pre+node->val; if(node->left==nullptr&&node->right==nullptr) { if(sum==res) return 1; else return 0; } return dfs(node->left,sum,res)|dfs(node->right,sum,res); } bool hasPathSum(TreeNode* root, int sum) { if(root==nullptr) return false; return dfs(root,0,sum); } };
C++ 解法, 执行用时: 3ms, 内存消耗: 768KB, 提交时间: 2022-03-11
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @param sum int整型 * @return bool布尔型 */ bool hasPathSum(TreeNode* root, int sum) { // write code here if(!root) return false; if(!root->left && !root->right){ if(sum==root->val) return true; else return false; } bool ans = hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val); return ans; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 768KB, 提交时间: 2021-11-29
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @param sum int整型 * @return bool布尔型 */ bool preOrder(TreeNode *node, int sum, int ans) { if(!node) return false; ans += node->val; if(!node->left && !node->right && sum == ans) return true; return preOrder(node->left, sum, ans)|| preOrder(node->right, sum ,ans); } bool hasPathSum(TreeNode* root, int sum) { // write code here if(!root) return false; return preOrder(root, sum, 0); } };