使用的BFS,在搜索过程中给res加上叶子结点的路径值。每个节点的路径值用一个队列来存储,之前值*10+子结点值

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    public int sumNumbers (TreeNode root) {
        // write code here
        if(root == null){
            return 0;
        }
        Queue<TreeNode> q = new LinkedList<>();
        Queue<Integer> num = new LinkedList<>();
        int res = 0;
        //num.offer(root.val);
        q.offer(root);
        num.offer(root.val);
       // System.out.println('1');
        while(!q.isEmpty()){
            int count = q.size();
            while(count-->0){
            int value = num.poll();
            TreeNode t = q.poll();
            //num.poll();
            if(t.left == null && t.right == null){
                    res = value + res; 
            }else{
                if(t.left != null){
                q.offer(t.left);
                num.offer(value*10+t.left.val);
                }
                if(t.right != null){
                q.offer(t.right);
                num.offer(value*10+t.right.val);
                }
              }
            }                   
        }
        return res;
    }
}