import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
int count = 0;
/**
*
* @param root TreeNode类
* @return int整型
*/
public int sumNumbers (TreeNode root) {
if(root==null) return 0;
dg(root, new ArrayList<>());
return count;
}
private void dg (TreeNode root, List<Integer> lj) {
if(root==null) return;
lj.add(root.val);
if(root.left==null && root.right==null){ // 叶子节点,计算路径值
int sum = 0;
int cj = 1;
for (int i = lj.size()-1; i >= 0; i--) {
sum += lj.get(i) * cj;
cj*=10;
}
count += sum;
}
if(root.left!=null) dg(root.left, lj); // 遍历左子节点
if(root.right!=null) dg(root.right, lj); // 遍历右子节点
lj.remove(lj.size() - 1);
}
}