Skip to content

Commit 1cf946a

Browse files
authored
Create Solution.java
1 parent d2d8670 commit 1cf946a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int sumRootToLeaf(TreeNode root) {
3+
return dfs(root, 0);
4+
}
5+
6+
private int dfs(TreeNode root, int s) {
7+
if (root == null) {
8+
return 0;
9+
}
10+
s = s << 1 | root.val;
11+
if (root.left == null && root.right == null) {
12+
return s;
13+
}
14+
return dfs(root.left, s) + dfs(root.right, s);
15+
}
16+
}

0 commit comments

Comments
 (0)