-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path257. Binary Tree Paths.cpp
More file actions
37 lines (31 loc) · 896 Bytes
/
Copy path257. Binary Tree Paths.cpp
File metadata and controls
37 lines (31 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//
// 257. Binary Tree Paths.cpp
// leetcode
//
// Created by R Z on 2017/9/27.
// Copyright © 2017年 R Z. All rights reserved.
//
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
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;
if(root) travel(root, "", res);
return res;
}
void travel(TreeNode* root, string path, vector<string> &res){
if(root->right == NULL && root->left == NULL) res.push_back(path+to_string(root->val));
if(root->left) travel(root->left, path+to_string(root->val)+"->", res);
if(root->right) travel(root->right,path+to_string(root->val)+"->", res);
//return sl+sr;
}
};