[leetcode] 113. Path Sum II (Medium)
阿新 • • 發佈:2018-11-08
跟112多了一點就是儲存路徑
依然用dfs,多了兩個vector儲存路徑
Runtime: 16 ms, faster than 16.09% of C++
class Solution { public: vector<vector<int>> res; vector<int> temp; vector<vector<int>> pathSum(TreeNode *root, int sum) { dfs(root,temp,0,sum);return res; } void dfs(TreeNode *root, vector<int> cur, int total, int sum) { if (root == NULL) return; total += root->val; cur.push_back(root->val); if (root->left == NULL && root->right == NULL) { if (total == sum) { res.push_back(cur); }return; } if (root->left) dfs(root->left, cur, total, sum); if (root->right) dfs(root->right, cur, total, sum); } };