-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumRootOfLeafNumbers.java
68 lines (59 loc) · 1.31 KB
/
SumRootOfLeafNumbers.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
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
60
61
62
63
64
65
66
67
68
package leetcode;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
*
* @author HJH
*
*/
public class SumRootOfLeafNumbers {
List<Integer> list = new LinkedList<>(); // 记录最后相加的结点
// 定义树结构,值,左子树,右子树
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public void getPath(TreeNode root, Stack<Integer> stack) {
stack.push(root.val);
if (root.left == null && root.right == null) { // 叶子节点
StringBuffer sb = new StringBuffer();
for (Integer integer : stack) {
sb.append(integer);
}
list.add(Integer.parseInt(sb.toString()));
}
// 存在左子树
if (root.left != null) {
getPath(root.left, stack);
}
// 存在右子树
if (root.right != null) {
getPath(root.right, stack);
}
stack.pop();
}
// 获取最后的相加和
public int sumNumbers(TreeNode root) {
if (root!=null) {
Stack<Integer> stack = new Stack<>();
getPath(root, stack);
}
int sum = 0;
for (Integer integer : list) {
sum += integer;
}
return sum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
System.out.println(new SumRootOfLeafNumbers().sumNumbers(root));
}
}