输出dfs二叉树路径。
刚刷leetcode,跟ACM提交方式不一样,适应中。。。
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> ans;
if(root == NULL) return ans;
dfs(ans, root, to_string(root->val));
return ans;
}
void dfs(vector<string> &ans, TreeNode* root, string s) {
if(root->left==NULL && root->right==NULL){
ans.push_back(s);
return;
}
if(root->left) dfs(ans, root->left, s+"->"+to_string(root->left->val));
if(root->right) dfs(ans, root->right, s+"->"+to_string(root->right->val));
}
};