题目
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
分析
题目不难,考察vector以及树的遍历。思路就是简单的深度优先搜索+维护一个vector即可。
不过这道题目有一些细节还是值得注意的,我在第一次AC后,共提交了三个版本才获得满意的时间消耗。
Solution 1:
class Solution1 { /*AC-1, 65ms*/
public:
vector<vector<int>> vec_result;
vector<vector<int>> pathSum(TreeNode *root, int sum) {
vector<int> initialVector;
vec_result.clear();
onepath(root,sum,initialVector);
return vec_result;
}
void onepath(TreeNode *root, int sum,vector<int> father) {
if(root==NULL) /* because child may be empty, it can be empty */
return;
/* judge if reach the leaf */
if(root->right==NULL&&root->left==NULL){
if(root->val==sum){
father.push_back(root->val);
vec_result.push_back(father);
}
return;
}
father.push_back(root->val);
sum-=root->val;
onepath(root->left,sum,father);
onepath(root->right,sum,father);
}
};
第一版的答案消耗时间达到65ms!
我想算法不应该有什么问题,因为这道题本身就不是考察算法的。那么原因必然出在vector的问题上,可能是频繁调用vector的相关操作消耗大量时间。
所以考虑是否维护一个更高效率的queue来存储遍历的值?所以就有了下面的第二版本
Solution 2:
与第一个版本的区别在于,维护一个queue,直到找到满足条件的路径才创建一个vector,将其加入结果。
class Solution2 { /* use queue, still very sllow */
public:
vector<vector<int>> vec_result;
vector<vector<int>> pathSum(TreeNode *root, int sum) {
queue<int> history;
onepath(root,sum,history);
return vec_result;
}
void onepath(TreeNode *root, int sum, queue<int> father) {
if(root==NULL) /* because child may be empty, it can be empty */
return;
/* judge if reach the leaf */
if(root->right==NULL&&root->left==NULL){
if(root->val==sum){
vector<int> findit;
while(!father.empty()){
int value = father.front();
findit.push_back(value);
father.pop();
}
findit.push_back(sum);
vec_result.push_back(findit);
}
return;
}
father.push(root->val);
sum-=root->val;
onepath(root->left,sum,father);
onepath(root->right,sum,father);
}
};
结果是时间消耗并没有显著减少!
那么就代表——vector和queue的操作效率没有想象中的那么大的差距,不是影响时间的主要原因!
问题处在什么上呢?
经过思考后我觉得整个过程已经没什么盲点,但是还是不能找出优化的方法。
在浏览DISCUSS的时候,发现一个18ms的解法中,对于临时的vector一直保持维护而不是在内层函数中创建一个副本!
我想我找到了解决的办法!
Solution 3:
引用(&) 还是创建一份拷贝?
class Solution {
public:
vector<vector<int>> vec_result;
vector<vector<int>> pathSum(TreeNode *root, int sum) {
vector<int> initialVector;
onepath(root,sum,initialVector);
return vec_result;
}
void onepath(TreeNode *root, int sum,<strong>vector<int> &father</strong>) {
if(root==NULL) /* because child may be empty, it can be empty */
return;
/* judge if reach the leaf */
if(root->right==NULL&&root->left==NULL){
if(root->val==sum){
father.push_back(root->val);
vec_result.push_back(father);
<strong> father.pop_back();</strong>
}
return;
}
father.push_back(root->val);
sum-=root->val;
onepath(root->left,sum,father);
onepath(root->right,sum,father);
<strong> father.pop_back();</strong>
return;
}
};
基于Solution1,代码3的区别仅仅是加粗的3行。
仔细想来,二者的区别就是:
如果是不加引用,则需要额外执行在每个子函数中创建一份拷贝的步骤!
而这一步设计内存分配、数据复制的过程,相比于调用pop_back()函数,当然要消耗更多的时间!
总结
最近在读《CSAPP》一书,获益最多的还是汇编部分对于函数效率的阐述。正在努力在代码中的小细节提高效率。
而这道问题,则是从一个相对不同的角度来说明形参对于效率的影响。
之前对于数据在内存中的拷贝没有多考虑,现在已经明确:这种操作是需要消耗较多时间的!
何况频繁申请空间深拷贝一份vector的弊端还更多啊,譬如产生内存碎片之类!也是我在这样的问题上欠考虑。