Path Sum Solutions in C++
Number 112
Difficulty Easy
Acceptance 41.2%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/path-sum/// Author : Hao Chen// Date : 2014-06-22#include <time.h>/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public:Solution(){srand(time(NULL));}bool hasPathSum(TreeNode *root, int sum) {return hasPathSum3(root, sum, 0);return hasPathSum2(root, sum);return hasPathSum1(root, sum);}bool hasPathSum3(TreeNode* root, int sum, int s) {if ( root == NULL) return false;s += root->val;if ( !root->left && !root->right) return s == sum;return (hasPathSum3(root->left, sum, s) || hasPathSum3(root->right, sum, s));}bool hasPathSum1(TreeNode *root, int sum) {if (root==NULL) return false;vector<TreeNode*> v;v.push_back(root);while(v.size()>0){TreeNode* node = v.back();v.pop_back();if (node->left==NULL && node->right==NULL){if (node->val == sum){return true;}}if (node->left){node->left->val += node->val;v.push_back(node->left);}if (node->right){node->right->val += node->val;v.push_back(node->right);}}return false;}bool hasPathSum2(TreeNode *root, int sum) {if (root==NULL) return false;if (root->left==NULL && root->right==NULL ){return (root->val==sum);}if (root->left){root->left->val += root->val;if (hasPathSum2(root->left, sum)){return true;}}if (root->right){root->right->val += root->val;if (hasPathSum2(root->right, sum)){return true;}}return false;}};
C++ solution by pezy/LeetCode
#include <cstddef>struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:bool hasPathSum(TreeNode *root, int sum) {if (!root) return false;if (!root->left and !root->right and sum-root->val==0) return true;return hasPathSum(root->left, sum-root->val) or hasPathSum(root->right, sum-root->val);}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/path-sum/description//// Author : liuyubobobo/// Time : 2017-11-17#include <iostream>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Recursive/// Time Complexity: O(n), where n is the nodes' number of the tree/// Space Complexity: O(h), where h is the height of the treeclass Solution {public:bool hasPathSum(TreeNode* root, int sum) {if(!root)return false;if(!root->left && !root->right)return sum == root->val;return hasPathSum(root->left, sum - root->val)|| hasPathSum(root->right, sum - root->val);}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/path-sum/description//// Author : liuyubobobo/// Time : 2018-11-17#include <iostream>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};/// Non-Recursive/// Using Stack////// Time Complexity: O(n), where n is the nodes' number of the tree/// Space Complexity: O(h), where h is the height of the treeclass Solution {public:bool hasPathSum(TreeNode* root, int sum) {if(!root)return false;stack<pair<TreeNode*, int>> stack;stack.push(make_pair(root, sum));while(!stack.empty()){TreeNode* cur = stack.top().first;int num = stack.top().second;stack.pop();if(num == cur->val && !cur->left && !cur->right)return true;if (cur->left)stack.push(make_pair(cur->left, num - cur->val));if (cur->right)stack.push(make_pair(cur->right, num - cur->val));}return false;}};int main() {return 0;}