- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsumRootToLeafNumbers.java
More file actions
Latest commit
33 lines (30 loc) · 734 Bytes
/
Copy pathsumRootToLeafNumbers.java
File metadata and controls
33 lines (30 loc) · 734 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
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
publicclassSolution {
publicintdfs(TreeNoderoot, intsum) {
if(root==null) {
return0;
}
if(root.left==null&&root.right==null) {
return10*sum + root.val;
}
intresult = 0;
if(root.left!=null) {
result += dfs(root.left, 10*sum + root.val);
}
if(root.right!=null) {
result += dfs(root.right, 10*sum + root.val);
}
returnresult;
}
publicintsumNumbers(TreeNoderoot) {
returndfs(root,0);
}
}