1

Please help me understand the difference. Like in first code snippet, I'm passing the updated ArrayList as a parameter in the recursive function in Line 1 & 2. This is giving me wrong answer.

public static void rootToLeafPaths(TreeNode node, ArrayList<Integer> sub){
        if(node == null)
            return;
        sub.add(node.val);
        if(node.left == null && node.right == null){
            list.add(sub);
            return;
        }
        rootToLeafPaths(node.left,sub); //LINE 1
        rootToLeafPaths(node.right,sub); //LINE 2
        sub.remove(sub.size()-1);    
    }

In the second code snippet I'm passing a newly created ArrayList(in Line 3 & 4) which is giving me the correct answer.

public static void rootToLeafPaths(TreeNode node, ArrayList<Integer> sub){
            if(node == null)
                return;
            sub.add(node.val);
            if(node.left == null && node.right == null){
                list.add(sub);
                return;
            }
            rootToLeafPaths(node.left,new ArrayList<Integer> (sub)); // LINE 3
            rootToLeafPaths(node.right,new ArrayList<Integer> (sub)); // LINE 4
            sub.remove(sub.size()-1);    
    }

Help me understand the difference.

This method is supposed to provide the root to leaf paths of a binary tree. Like in first case it is giving me [[1, 2, 4], [1, 2, 4], [1, 2, 4]] as output which is wrong, but when I'm passing a newly created ArrayList each time it is giving me [[1, 2, 4, 6, 8], [1, 2, 5, 7, 9, 10], [1, 3]] as output which is correct

Short Summary(Based on answers): Even if we assign list to some other list(Example: List list = list2), we're assigning the reference to it. So both will be pointing to same list. And changes in one will be reflected in another. So to solve this we'll need to create another list by (List list = new ArrayList(list2);)

4
  • What correct answer? What is the method supposed to do? Commented Apr 29, 2021 at 6:24
  • @khelwood This method is supposed to provide the root to leaf paths of a binary tree. Like in first case it is giving me [[1, 2, 4], [1, 2, 4], [1, 2, 4]] as output which is wrong, but when I'm passing a newly created ArrayList each time it is giving me [[1, 2, 4, 6, 8], [1, 2, 5, 7, 9, 10], [1, 3]] as output which is correct. Commented Apr 29, 2021 at 6:28
  • That is information that should be in your question. Commented Apr 29, 2021 at 6:30
  • 1
    @khelwood Thanks. I've edited my ques for now. From next time I'll keep that in my mind before posting. Commented Apr 29, 2021 at 6:35

1 Answer 1

2

In list.add(sub) you are adding sub List as an element of list List.

In your first snippet, sub always references the same List, which means your output list will contain multiple references to the same List. Hence the wrong answer.

In your second snippet, you create a copy of sub for each recursive call, which means you are adding different List instances to your list.

Sign up to request clarification or add additional context in comments.

2 Comments

Just to be clear with my understanding, what you mean to say is like in first code snippet output List will have reference to the same ArrayList(sub). Right? But I have one more doubt what it will contain like the lastly generated elements will be present in each of the list in output list. Like it contains 1,2,4 for this output [[1, 2, 4], [1, 2, 4], [1, 2, 4]]
@ParundeepSingh the lastly generated elements will be present in each of the list in output list - there's just one list in the output list (or, to be exact, the same list was added multiple times to the output list). Therefore you see the same elements printed multiple times. Yes, sub contains the elements 1,2,4 at the end of the execution of your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.