Is the time complexity of my code O(n2)?


  • 0
     public int pathSum(TreeNode root, int sum) {
          List<Integer> list = new LinkedList<Integer>();
          return pathSum(root,list,sum,0);
        }
        
        public int pathSum(TreeNode root, List<Integer> list, int givenSum, int count)
    	    {
    	        if(root == null)
    	          return count;
    	        if(root.val == givenSum)
    	          count++;
    	        if(list.size() >= 1)
    	        {
    	            int tempSum = root.val;
    	            for(Integer i : list)
    	            {
    	                tempSum += i;
    	                if(tempSum == givenSum)
    	                  count++;
    	            }
    	        }
    	        list.add(0,root.val);
    	        count = pathSum(root.left, list, givenSum, count);
    	        count = pathSum(root.right,list,givenSum, count);
    	        list.remove(0);
    	        return count;
    	    }```

Log in to reply
 

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