-
Notifications
You must be signed in to change notification settings - Fork 0
/
129.java
30 lines (27 loc) · 819 Bytes
/
129.java
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
// https://leetcode.com/problems/sum-root-to-leaf-numbers/?envType=study-plan-v2&envId=top-interview-150
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public static int dfs(TreeNode cur, int path) {
if (cur == null) return 0;
int next = path * 10 + cur.val;
if (cur.left == null && cur.right == null) return next;
return dfs(cur.left, next) + dfs(cur.right, next);
}
public int sumNumbers(TreeNode root) {
return dfs(root, 0);
}
}