Why my question get compile error?


  • 0
    L

    i have test the code on local machine,but the website gives me an wrong answer,give one helps?
    the solution is recursive the tree from the root to the leaf node , if the node isn't a leaf node ,we put them in a linkedList ,otherwise ,we judge the sum from root to the leaf is equals to the sum .anyone would helps?

    import java.util.LinkedList;
    
    public class Solution{
    
    public static LinkedList<LinkedList<Integer>>  pathSum(TreeNode root,int sum)
    {	
    LinkedList<LinkedList<Integer>> result=new  LinkedList<>();
    if(root==null)
    {	
    return result;
    }
    result.addAll(bfs(root,sum,new  LinkedList<>(),0));
    return  result; 
    }
    private  static LinkedList<LinkedList<Integer>>  bfs(TreeNode root,int sum,LinkedList<Integer>list,int count)
    {
    LinkedList<LinkedList<Integer>> result=new LinkedList<>(); 
    if(root.left==null&&root.right==null)
    {
    if((root.val+count)==sum)	
    {
    list.addLast(root.val);	
    result.add(list);
    }
    return result;  
    }
    list.add(root.val);
    if(root.left!=null)
    {
    result.addAll(bfs(root.left, sum,new LinkedList<Integer>(list),count+root.val));	
    }
    if(root.right!=null)
    {
     result.addAll( bfs(root.right,sum,new LinkedList<>(list), count+root.val));		
    }
    return result;
    } 
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.