week26
257 - 二叉树的所有路径
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 - 各位相加
260 - 只出现一次的数字III

Last updated
