week26
257 - 二叉树的所有路径
基础dfs题
class Solution {
public:
vector<string> res;
public:
vector<string> binaryTreePaths(TreeNode* root) {
if(root == nullptr) return res;
vector<int> path;
dfs(root, path);
return res;
}
void dfs(TreeNode* root, vector<int>& path)
{
if(root->left == nullptr && root->right == nullptr)
{
stringstream ss;
for(int x : path)
ss << to_string(x) << "->";
ss << to_string(root->val);
res.push_back(ss.str());
return;
}
// * left 和 right
path.push_back(root->val);
if(root->left) dfs(root->left, path);
if(root->right) dfs(root->right, path);
path.pop_back();
}
};258 - 各位相加
你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗?
o(1)是不是有公式呀..
同余 数论真的好难... 
260 - 只出现一次的数字III
复习一下前两题是怎么做的
你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?

Last updated