We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 1340e22 + 84fb47e commit 597da2cCopy full SHA for 597da2c
leetcode/cpp/Tree/129.cpp
@@ -0,0 +1,30 @@
1
+//Sum root to leaf numbers
2
+
3
+/**
4
+ * Definition for a binary tree node.
5
+ * struct TreeNode {
6
+ * int val;
7
+ * TreeNode *left;
8
+ * TreeNode *right;
9
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
10
+ * };
11
+ */
12
+class Solution {
13
+public:
14
+ int dfs(int parent, TreeNode* root){
15
+ int cur = parent*10+root->val;
16
+ int sum = 0;
17
+ if(root->left == NULL && root->right == NULL)
18
+ return cur;
19
+ if(root->left!=NULL)
20
+ sum = dfs(cur,root->left);
21
+ if(root->right!=NULL)
22
+ sum+=dfs(cur,root->right);
23
+ return sum;
24
+ }
25
+ int sumNumbers(TreeNode* root) {
26
+ if(root == NULL)
27
+ return 0;
28
+ return dfs(0,root);
29
30
+};
0 commit comments