1: /** 2: * Definition for a binary tree node. 3: * struct TreeNode { 4: * int val; 5: * TreeNode *left; 6: * TreeNode *right; 7: * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8: * }; 9: */ 10: class Solution { 11: public: 12: vector<vector<int>> pathSum(TreeNode* root, int sum) { 13: vector<vector<int>> res; 14: vector<int> sol; 15: helper(root, sum, sol, res); 16: return res; 17: } 18: void helper(TreeNode *root, int sum, vector<int> &sol, vector<vector<int>> &res) { 19: if (root == NULL) return; 20: if (root->left == NULL && root->right == NULL) { 21: if (sum == root->val) { 22: sol.push_back(root->val); 23: res.push_back(sol); 24:
sol.pop_back();
25: } 26: return; 27: } 28: sol.push_back(root->val); 29: helper(root->left, sum-root->val, sol, res); 30: helper(root->right, sum-root->val, sol, res); 31: sol.pop_back(); 32: } 33: };
Wednesday, August 10, 2016
113. Path Sum II
Don't forget line 24.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment