-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
62 lines (57 loc) · 1.27 KB
/
Copy pathmain.cpp
File metadata and controls
62 lines (57 loc) · 1.27 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// main.cpp
// leetcode129
//
// Created by 萧天牧 on 17/4/23.
// Copyright © 2017年 萧天牧. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int str2int(string s){
int result = 0;
for(int i = 0; i < s.length(); i++){
result *= 10;
result += s[i] - '0';
}
return result;
}
void countNum(TreeNode* root, string s, vector<string>& res){
if(!root)
return;
char c = root->val + '0';
string tmp = s + char(c);
if(!root -> left && !root -> right){
res.push_back(tmp);
return;
}
countNum(root -> left, tmp, res);
countNum(root -> right, tmp, res);
}
int sumNumbers(TreeNode* root) {
vector<string> res;
if (!root)
return 0;
int result = 0;
string s = "";
countNum(root,s, res);
for(int i = 0; i < res.size(); i++){
result += str2int(res[i]);
}
return result;
}
int main(int argc, const char * argv[]) {
TreeNode* root = new TreeNode(1);
root -> left =new TreeNode(2);
root -> right =new TreeNode(3);
cout << sumNumbers(root);
}