难度简单423
给定一个二叉树,返回所有从根节点到叶子节点的路径。
说明: 叶子节点是指没有子节点的节点。
示例:
输入: 1 / \ 2 3 \ 5 输出: ["1->2->5", "1->3"] 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3
这题需要再想想。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> paths;
constructPaths(root, "", paths);
return paths;
}
void constructPaths(TreeNode* root, string path, vector<string> & paths){
if(root != NULL){
path += to_string(root->val);
if(root->left == NULL && root->right == NULL){
paths.push_back(path);
}else{
path += "->";
constructPaths(root->left, path, paths);
constructPaths(root->right, path, paths);
}
}
}
};
换了一个题解看了看思路清晰了很多,但是不明白为什么下面的代码不能通过。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
vector<int> path;
if(root == NULL){
return res;
}
constructPaths(root, path, res);
return res;
}
void constructPaths(TreeNode* root, vector<int>& path, vector<string> & res){
path.push_back(root->val);
if(root->left == NULL && root->right){ //为叶子节点
string spath;
for (int i = 0; i < path.size() - 1; i++){
spath += to_string(path[i]);
spath += "->";
}
spath += to_string(path[path.size()-1]);
res.push_back(spath);
return;
}
if(root->left){
constructPaths(root->left, path, res);
path.pop_back();
}
if(root->right){
constructPaths(root->right, path, res);
path.pop_back(); //回溯
}
}
};
除了吃,我还能干点啥!!o(╥﹏╥)oo(╥﹏╥)o
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
void constructPaths(TreeNode* root, vector<int>& path, vector<string> & res){
path.push_back(root->val);
if(root->left == NULL && root->right == NULL){ //为叶子节点
string spath;
for (int i = 0; i < path.size() - 1; i++){
spath += to_string(path[i]);
spath += "->";
}
spath += to_string(path[path.size()-1]);
res.push_back(spath);
return;
}
if(root->left){
constructPaths(root->left, path, res);
path.pop_back();
}
if(root->right){
constructPaths(root->right, path, res);
path.pop_back(); //回溯
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
vector<int> path;
if(root == NULL){
return res;
}
constructPaths(root, path, res);
return res;
}
};
再看一遍,大致明白了,递归和回溯不分家,这个道理好难实现啊。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
void constructPaths(TreeNode* root, string path, vector<string> & res){
path += to_string(root->val); //中
if(root->left == NULL && root->right == NULL){ //为叶子节点
res.push_back(path);
return;
}
if(root->left){
constructPaths(root->left, path + "->", res);
}
if(root->right){
constructPaths(root->right, path + "->", res);
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
string path;
if(root == NULL){
return res;
}
constructPaths(root, path, res);
return res;
}
};