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 2ab9169 commit 2325bceCopy full SHA for 2325bce
Tree Depth First Search/129_Sum_Root_to_Leaf_Numbers.java
@@ -0,0 +1,24 @@
1
+class Solution {
2
+ public int sumNumbers(TreeNode root) {
3
+ if (root == null) {
4
+ return 0;
5
+ }
6
+ return dfs(root, 0);
7
8
+
9
+ private int dfs(TreeNode root, int levelBase) {
10
11
12
13
14
+ if (root.left == null && root.right == null) {
15
+ return levelBase + root.val;
16
17
18
+ int nextLevelBase = 10 * (levelBase + root.val);
19
+ int left = dfs(root.left, nextLevelBase);
20
+ int right = dfs(root.right, nextLevelBase);
21
22
+ return left + right;
23
24
+}
0 commit comments