-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_129.java
More file actions
59 lines (52 loc) · 1.12 KB
/
Solution_129.java
File metadata and controls
59 lines (52 loc) · 1.12 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.hilbert25.leetcode;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author : hilbert25
* @version 创建时间:2017年5月18日 上午9:55:48 LeetCode com.hilbert25.leetcode
* Solution_129
*/
public class Solution_129 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
/**
* @param root
* @return
*/
public int sumNumbers(TreeNode root) {
if (root == null)
return 0;
int sum = 0;
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
while (!nodeQueue.isEmpty()) {
int count = nodeQueue.size();
for (int i = 0; i < count; i++) {
TreeNode node = nodeQueue.poll();
if (node.left == null && node.right == null) {
sum += node.val;
} else {
node.val *= 10;
if (node.left != null) {
node.left.val += node.val;
nodeQueue.add(node.left);
}
if (node.right != null) {
node.right.val += node.val;
nodeQueue.add(node.right);
}
}
}
}
return sum;
}
}