leetcode 257. Binary Tree Paths
返回所有根到叶子的路径。用个“全局”的ret代替合的过程。
vector<string> binaryTreePaths(TreeNode* root) { if (root == NULL) return vector<string>(); vector<string> ret; dfs(root, string(""), ret); return ret; } void dfs(TreeNode* root, string now, vector<string>& ret) { if (root->left == NULL && root->right == NULL) { ret.push_back(now + to_string(root->val)); return; } now = now + to_string(root->val) + "->"; if (root->left) dfs(root->left, now, ret); if (root->right) dfs(root->right, now, ret); }
【本文章出自博客园willaty,转载请注明作者出处,误差欢迎指出~】