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.
1 parent d2d8670 commit 1cf946aCopy full SHA for 1cf946a
solution/1022.Sum of Root To Leaf Binary Numbers/Solution.java
@@ -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