Home » Java » java – Question about the technique used in solution for LeetCode 1110. Delete Nodes And Return Forest-Exceptionshub

java – Question about the technique used in solution for LeetCode 1110. Delete Nodes And Return Forest-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

LeetCode 1110. Delete Nodes And Return Forest
enter image description here

One of solutions is 6:14 in https://www.youtube.com/watch?time_continue=44&v=ZwlVb_sz-dE

part of her solution is

List<TreeNode> res = new ArrayList<>();
res.add(root);                            //<== how this works?
...

res.remove(cur);

My question is
root is added to res
then suppose 5 is needs to be deleted and if current node is 5
then res.remove(cur) is executed.

How come this this works?
res is ArrayList and contains only root and root.left.right is 5
when res.remove(cur) is executed, then ArrayList’s one element, root, automatically goes to its left and then right and then remove node 5 ???

I really don’t understand this part.
What’s going on when res.remove(cur) is executed internally?
Is there anyone able to help me to understand this part?

My question is not about the algorithm of this question.
res.remove(cur) is mystery for me.

enter image description here

How to&Answers: