public class Solution { /** * * @param root TreeNode类 * @return int整型 */ public int sumNumbers (TreeNode root) { // write code here List<List<Integer>> ans=new ArrayList<>(); List<Integer> path=new ArrayList<>(); backTrack(root,ans,path); int res=0; for(int i=0;i<ans.size();i++){ int sum=0; for(int j=0;j<ans.get(i).size();j++){ sum=sum*10+ans.get(i).get(j); } res+=sum; } return res; } void backTrack(TreeNode root,List<List<Integer>> ans,List<Integer> path){ if(root==null){ return; } path.add(root.val); if(root.left==null&&root.right==null){ ans.add(new ArrayList<>(path)); }else{ backTrack(root.left,ans,path); backTrack(root.right,ans,path); } path.remove(path.size()-1); } }
点赞 1

相关推荐

06-04 17:59
已编辑
长江大学 Java
点赞 评论 收藏
分享
牛客583549203号:腾讯还好,况且实习而已,实习生流动性很大,属于正常现象,记得和HR委婉解释
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务