-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path129.cpp
More file actions
31 lines (29 loc) · 708 Bytes
/
129.cpp
File metadata and controls
31 lines (29 loc) · 708 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
//
// 129.cpp
// leetcode
//
// Created by R Z on 2018/8/11.
// Copyright © 2018年 R Z. All rights reserved.
//
#include <stdio.h>
/**
* Definition for a binary tree node.*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
private:
int res=0;
public:
int sumNumbers(TreeNode* root) {
return sumNumbersHelp(root,0);
}
int sumNumbersHelp(TreeNode* node, int n){
if(!node) return 0;
if(node->left==NULL && node->right==NULL) return n*10+node->val;
return sumNumbersHelp(node->left,n*10+node->val)+sumNumbersHelp(node->right,n*10+node->val);
}
};