# 257. Binary Tree Paths

Given a binary tree, return all root-to-leaf paths.

Note: A leaf is a node with no children.

Example:

Input:

1
/   \
2     3
\
5

Output: ["1->2->5", "1->3"]


## Solution

### DFS

Runtime: 4 ms, faster than 79.62% of C++ online submissions for Binary Tree Paths.
Memory Usage: 14 MB, less than 5.26% of C++ online submissions for Binary Tree Paths.

class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(!root) return res;
getPath(res, root, to_string(root->val));
return res;
}
private:
void getPath(vector<string>& s, TreeNode* root, string t){
if(!root->left && !root->right){
s.push_back(t);
return;
}
if(root->left) getPath(s, root->left, t+"->"+to_string(root->left->val));
if(root->right) getPath(s, root->right, t+"->"+to_string(root->right->val));
}
};


07-23 252

03-11 76

06-18 5683

08-17 696

07-27 73

09-06 51

05-30 118

04-23 15

07-20 11

01-10 2034

02-20 723

06-11 782

10-08 54

02-20 30

10-07 119

04-14 59万+

03-13 15万+

03-04 14万+

03-08 2万+

04-25 6万+

03-10 13万+

03-10 18万+

03-12 11万+

03-13 11万+

03-18 9915

03-19 8万+

03-19 3万+

03-22 4万+

03-23 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-29 21万+

03-29 10万+

03-30 16万+

05-31 6308

05-31 1万+

04-02 1万+

04-02 5641

04-02 4万+

04-06 1万+

04-06 7万+

04-09 8万+

04-09 2万+

05-17 8568

04-11 3万+

04-15 6万+

04-18 4万+

04-20 4万+

04-24 3万+

05-01 6433

05-03 1万+

05-16 5万+

05-06 1万+

05-07 2729

05-07 1万+

05-08 4万+

05-10 3310

05-14 6524

05-16 3601

05-16 1万+

05-17 5083

05-30 371

05-20 1002

05-18 2254

05-18 8429

05-27 1452

05-18 3617

05-28 1292

05-19 1万+

05-19 1943

05-21 7596

05-21 7622

05-27 5209

05-25 5396

05-23 6553

05-28 1696

#### 因为无聊，我做出了一个能下载电影、动漫、电视剧的小程序，从此告别过去只能看部分视频简介的历史

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客