Iterate through nested List to return inner List by specified param
The goal is to return the inner list of Object with a given param from the data list. The layer on which this Object will be found may vary.
The pseudo code for the Object is
class Object {
var id: Int
var innerObjects: ArrayList<Object>
}
If innerObjects
param is null then we reached the leaf.
We are given ArrayList<Object>
which is the data and id
we are searching for. The Object we search can be on any level of the list.
What I have now is this piece of code but it only checks for 2 levels of the list. Please, suggest a solution that would work for any number of levels in the list. Possibly with recursion.
private fun getObject(data: ArrayList<Object>, id: Int): ArrayList<Object> {
var result = ArrayList<Object>()
for (i in 0 until data.size) {
if (data[i].id == id) {
result = data[i].innerObjects
} else {
for (j in 0 until data[i].innerObjects.size) {
if (data[i].innerObjects[j].id == id) {
result = data[i].innerObjects[j].innerObjects
}
}
}
}
return result
}
By the way, the code is in Kotlin but feel free to suggest solution in Kotlin or Java.
java algorithm kotlin depth-first-search breadth-first-search
add a comment |
The goal is to return the inner list of Object with a given param from the data list. The layer on which this Object will be found may vary.
The pseudo code for the Object is
class Object {
var id: Int
var innerObjects: ArrayList<Object>
}
If innerObjects
param is null then we reached the leaf.
We are given ArrayList<Object>
which is the data and id
we are searching for. The Object we search can be on any level of the list.
What I have now is this piece of code but it only checks for 2 levels of the list. Please, suggest a solution that would work for any number of levels in the list. Possibly with recursion.
private fun getObject(data: ArrayList<Object>, id: Int): ArrayList<Object> {
var result = ArrayList<Object>()
for (i in 0 until data.size) {
if (data[i].id == id) {
result = data[i].innerObjects
} else {
for (j in 0 until data[i].innerObjects.size) {
if (data[i].innerObjects[j].id == id) {
result = data[i].innerObjects[j].innerObjects
}
}
}
}
return result
}
By the way, the code is in Kotlin but feel free to suggest solution in Kotlin or Java.
java algorithm kotlin depth-first-search breadth-first-search
You need to use a graph traversal algorithm, such as depth-first search or breadth-first search.
– ruakh
Nov 22 '18 at 1:45
@ruakh looked into those but the problem I faced that my return breaks the recursion. In the examples I found the algorithms don't return specific Node. Could you provide an example with the return?
– Rainmaker
Nov 22 '18 at 2:23
add a comment |
The goal is to return the inner list of Object with a given param from the data list. The layer on which this Object will be found may vary.
The pseudo code for the Object is
class Object {
var id: Int
var innerObjects: ArrayList<Object>
}
If innerObjects
param is null then we reached the leaf.
We are given ArrayList<Object>
which is the data and id
we are searching for. The Object we search can be on any level of the list.
What I have now is this piece of code but it only checks for 2 levels of the list. Please, suggest a solution that would work for any number of levels in the list. Possibly with recursion.
private fun getObject(data: ArrayList<Object>, id: Int): ArrayList<Object> {
var result = ArrayList<Object>()
for (i in 0 until data.size) {
if (data[i].id == id) {
result = data[i].innerObjects
} else {
for (j in 0 until data[i].innerObjects.size) {
if (data[i].innerObjects[j].id == id) {
result = data[i].innerObjects[j].innerObjects
}
}
}
}
return result
}
By the way, the code is in Kotlin but feel free to suggest solution in Kotlin or Java.
java algorithm kotlin depth-first-search breadth-first-search
The goal is to return the inner list of Object with a given param from the data list. The layer on which this Object will be found may vary.
The pseudo code for the Object is
class Object {
var id: Int
var innerObjects: ArrayList<Object>
}
If innerObjects
param is null then we reached the leaf.
We are given ArrayList<Object>
which is the data and id
we are searching for. The Object we search can be on any level of the list.
What I have now is this piece of code but it only checks for 2 levels of the list. Please, suggest a solution that would work for any number of levels in the list. Possibly with recursion.
private fun getObject(data: ArrayList<Object>, id: Int): ArrayList<Object> {
var result = ArrayList<Object>()
for (i in 0 until data.size) {
if (data[i].id == id) {
result = data[i].innerObjects
} else {
for (j in 0 until data[i].innerObjects.size) {
if (data[i].innerObjects[j].id == id) {
result = data[i].innerObjects[j].innerObjects
}
}
}
}
return result
}
By the way, the code is in Kotlin but feel free to suggest solution in Kotlin or Java.
java algorithm kotlin depth-first-search breadth-first-search
java algorithm kotlin depth-first-search breadth-first-search
edited Nov 22 '18 at 18:39
Jayson Minard
40.2k17112173
40.2k17112173
asked Nov 22 '18 at 1:29
RainmakerRainmaker
3,23131540
3,23131540
You need to use a graph traversal algorithm, such as depth-first search or breadth-first search.
– ruakh
Nov 22 '18 at 1:45
@ruakh looked into those but the problem I faced that my return breaks the recursion. In the examples I found the algorithms don't return specific Node. Could you provide an example with the return?
– Rainmaker
Nov 22 '18 at 2:23
add a comment |
You need to use a graph traversal algorithm, such as depth-first search or breadth-first search.
– ruakh
Nov 22 '18 at 1:45
@ruakh looked into those but the problem I faced that my return breaks the recursion. In the examples I found the algorithms don't return specific Node. Could you provide an example with the return?
– Rainmaker
Nov 22 '18 at 2:23
You need to use a graph traversal algorithm, such as depth-first search or breadth-first search.
– ruakh
Nov 22 '18 at 1:45
You need to use a graph traversal algorithm, such as depth-first search or breadth-first search.
– ruakh
Nov 22 '18 at 1:45
@ruakh looked into those but the problem I faced that my return breaks the recursion. In the examples I found the algorithms don't return specific Node. Could you provide an example with the return?
– Rainmaker
Nov 22 '18 at 2:23
@ruakh looked into those but the problem I faced that my return breaks the recursion. In the examples I found the algorithms don't return specific Node. Could you provide an example with the return?
– Rainmaker
Nov 22 '18 at 2:23
add a comment |
3 Answers
3
active
oldest
votes
I have written java code for the solution :
ArrayList<TreeNode> solve(TreeNode treeNode, int id) {
if(treeNode == null) return null;
if(treeNode.id == id) return treeNode.nodes;
if(treeNode.nodes == null) return null;
ArrayList<TreeNode> ans = null, temp = null;
for(TreeNode t: treeNode.nodes) {
temp = solve(t, id);
if(temp != null) ans = temp;
}
return ans;
}
Demo on Ideone : https://ideone.com/cMmAqD
Thanks, for the answer, it helped me to figure out how to apply it for my case, I will accept your answer and post my code also for future reference!
– Rainmaker
Nov 22 '18 at 16:28
add a comment |
The solution I applied for my case in Kotlin for those who are interested!
private fun getObjectById(item: Object, id: Int): ArrayList<Object>? {
if (item.id == id) return item.innerObjects
for (child in item.innerObjects) {
val temp = getObjectById(child, id)
if (temp!!.isNotEmpty()) return temp
}
return ArrayList()
}
fun combineLists(data: ArrayList<Object>?, id: Int) {
for (j in 0 until data.size) {
getObjectById(data[j], id)?.let {
result.clear()
result.addAll(it)
}
}
}
add a comment |
So, in this case using basic recursion will solve this, but beware, because this kind of searching can quickly produce literal StackOverflowException
s.
fun getInnerObjectsById(nodes: ArrayList<Object>, id: Int) : ArrayList<Object>? {
nodes.forEach{
val ans = solve(it, id)
if (ans != null) return ans
}
return null
}
fun getInnerObjectsById(obj: Object?, id: Int): ArrayList<Object>? {
if (obj?.id == null || obj.innerObjects == null) return null
if (obj.id == id) return obj.innerObjects
return obj.innerObjects?.mapNotNull {
solve(it, id)
}?.firstOrNull()
}
This is a more involved, but imperative search.
fun getInnerObjectsById(data: ArrayList<Object>, id: Int): ArrayList<Object>? {
var activeList: List<Object> = data
while (true) {
val match = activeList.firstOrNull { it.id == id }
if (match != null) return match.innerObjects
activeList = activeList.flatMap { it.innerObjects ?: emptyList<Object>() }
if (!activeList.isNotEmpty())
return null
}
}
thanks for the answer, tried the first one but it doesn't find the Object by id. it only returns innerObjects of the last item in data no matter the id, and the second one doesn't compile as data is ArrayList and it doesn't have param id on line 4
– Rainmaker
Nov 22 '18 at 3:53
Finally had a chance to address this with a compiler in front of me, and have corrected my solutions. I would highly suggest not using the recursive search. And also, you're final solution and the recursive solution in general, are using nested loops, which are also less desirable.
– gtcompscientist
Nov 24 '18 at 19:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422696%2fiterate-through-nested-list-to-return-inner-list-by-specified-param%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have written java code for the solution :
ArrayList<TreeNode> solve(TreeNode treeNode, int id) {
if(treeNode == null) return null;
if(treeNode.id == id) return treeNode.nodes;
if(treeNode.nodes == null) return null;
ArrayList<TreeNode> ans = null, temp = null;
for(TreeNode t: treeNode.nodes) {
temp = solve(t, id);
if(temp != null) ans = temp;
}
return ans;
}
Demo on Ideone : https://ideone.com/cMmAqD
Thanks, for the answer, it helped me to figure out how to apply it for my case, I will accept your answer and post my code also for future reference!
– Rainmaker
Nov 22 '18 at 16:28
add a comment |
I have written java code for the solution :
ArrayList<TreeNode> solve(TreeNode treeNode, int id) {
if(treeNode == null) return null;
if(treeNode.id == id) return treeNode.nodes;
if(treeNode.nodes == null) return null;
ArrayList<TreeNode> ans = null, temp = null;
for(TreeNode t: treeNode.nodes) {
temp = solve(t, id);
if(temp != null) ans = temp;
}
return ans;
}
Demo on Ideone : https://ideone.com/cMmAqD
Thanks, for the answer, it helped me to figure out how to apply it for my case, I will accept your answer and post my code also for future reference!
– Rainmaker
Nov 22 '18 at 16:28
add a comment |
I have written java code for the solution :
ArrayList<TreeNode> solve(TreeNode treeNode, int id) {
if(treeNode == null) return null;
if(treeNode.id == id) return treeNode.nodes;
if(treeNode.nodes == null) return null;
ArrayList<TreeNode> ans = null, temp = null;
for(TreeNode t: treeNode.nodes) {
temp = solve(t, id);
if(temp != null) ans = temp;
}
return ans;
}
Demo on Ideone : https://ideone.com/cMmAqD
I have written java code for the solution :
ArrayList<TreeNode> solve(TreeNode treeNode, int id) {
if(treeNode == null) return null;
if(treeNode.id == id) return treeNode.nodes;
if(treeNode.nodes == null) return null;
ArrayList<TreeNode> ans = null, temp = null;
for(TreeNode t: treeNode.nodes) {
temp = solve(t, id);
if(temp != null) ans = temp;
}
return ans;
}
Demo on Ideone : https://ideone.com/cMmAqD
answered Nov 22 '18 at 4:48
uSeemSurpriseduSeemSurprised
1,63521017
1,63521017
Thanks, for the answer, it helped me to figure out how to apply it for my case, I will accept your answer and post my code also for future reference!
– Rainmaker
Nov 22 '18 at 16:28
add a comment |
Thanks, for the answer, it helped me to figure out how to apply it for my case, I will accept your answer and post my code also for future reference!
– Rainmaker
Nov 22 '18 at 16:28
Thanks, for the answer, it helped me to figure out how to apply it for my case, I will accept your answer and post my code also for future reference!
– Rainmaker
Nov 22 '18 at 16:28
Thanks, for the answer, it helped me to figure out how to apply it for my case, I will accept your answer and post my code also for future reference!
– Rainmaker
Nov 22 '18 at 16:28
add a comment |
The solution I applied for my case in Kotlin for those who are interested!
private fun getObjectById(item: Object, id: Int): ArrayList<Object>? {
if (item.id == id) return item.innerObjects
for (child in item.innerObjects) {
val temp = getObjectById(child, id)
if (temp!!.isNotEmpty()) return temp
}
return ArrayList()
}
fun combineLists(data: ArrayList<Object>?, id: Int) {
for (j in 0 until data.size) {
getObjectById(data[j], id)?.let {
result.clear()
result.addAll(it)
}
}
}
add a comment |
The solution I applied for my case in Kotlin for those who are interested!
private fun getObjectById(item: Object, id: Int): ArrayList<Object>? {
if (item.id == id) return item.innerObjects
for (child in item.innerObjects) {
val temp = getObjectById(child, id)
if (temp!!.isNotEmpty()) return temp
}
return ArrayList()
}
fun combineLists(data: ArrayList<Object>?, id: Int) {
for (j in 0 until data.size) {
getObjectById(data[j], id)?.let {
result.clear()
result.addAll(it)
}
}
}
add a comment |
The solution I applied for my case in Kotlin for those who are interested!
private fun getObjectById(item: Object, id: Int): ArrayList<Object>? {
if (item.id == id) return item.innerObjects
for (child in item.innerObjects) {
val temp = getObjectById(child, id)
if (temp!!.isNotEmpty()) return temp
}
return ArrayList()
}
fun combineLists(data: ArrayList<Object>?, id: Int) {
for (j in 0 until data.size) {
getObjectById(data[j], id)?.let {
result.clear()
result.addAll(it)
}
}
}
The solution I applied for my case in Kotlin for those who are interested!
private fun getObjectById(item: Object, id: Int): ArrayList<Object>? {
if (item.id == id) return item.innerObjects
for (child in item.innerObjects) {
val temp = getObjectById(child, id)
if (temp!!.isNotEmpty()) return temp
}
return ArrayList()
}
fun combineLists(data: ArrayList<Object>?, id: Int) {
for (j in 0 until data.size) {
getObjectById(data[j], id)?.let {
result.clear()
result.addAll(it)
}
}
}
answered Nov 22 '18 at 16:36
RainmakerRainmaker
3,23131540
3,23131540
add a comment |
add a comment |
So, in this case using basic recursion will solve this, but beware, because this kind of searching can quickly produce literal StackOverflowException
s.
fun getInnerObjectsById(nodes: ArrayList<Object>, id: Int) : ArrayList<Object>? {
nodes.forEach{
val ans = solve(it, id)
if (ans != null) return ans
}
return null
}
fun getInnerObjectsById(obj: Object?, id: Int): ArrayList<Object>? {
if (obj?.id == null || obj.innerObjects == null) return null
if (obj.id == id) return obj.innerObjects
return obj.innerObjects?.mapNotNull {
solve(it, id)
}?.firstOrNull()
}
This is a more involved, but imperative search.
fun getInnerObjectsById(data: ArrayList<Object>, id: Int): ArrayList<Object>? {
var activeList: List<Object> = data
while (true) {
val match = activeList.firstOrNull { it.id == id }
if (match != null) return match.innerObjects
activeList = activeList.flatMap { it.innerObjects ?: emptyList<Object>() }
if (!activeList.isNotEmpty())
return null
}
}
thanks for the answer, tried the first one but it doesn't find the Object by id. it only returns innerObjects of the last item in data no matter the id, and the second one doesn't compile as data is ArrayList and it doesn't have param id on line 4
– Rainmaker
Nov 22 '18 at 3:53
Finally had a chance to address this with a compiler in front of me, and have corrected my solutions. I would highly suggest not using the recursive search. And also, you're final solution and the recursive solution in general, are using nested loops, which are also less desirable.
– gtcompscientist
Nov 24 '18 at 19:23
add a comment |
So, in this case using basic recursion will solve this, but beware, because this kind of searching can quickly produce literal StackOverflowException
s.
fun getInnerObjectsById(nodes: ArrayList<Object>, id: Int) : ArrayList<Object>? {
nodes.forEach{
val ans = solve(it, id)
if (ans != null) return ans
}
return null
}
fun getInnerObjectsById(obj: Object?, id: Int): ArrayList<Object>? {
if (obj?.id == null || obj.innerObjects == null) return null
if (obj.id == id) return obj.innerObjects
return obj.innerObjects?.mapNotNull {
solve(it, id)
}?.firstOrNull()
}
This is a more involved, but imperative search.
fun getInnerObjectsById(data: ArrayList<Object>, id: Int): ArrayList<Object>? {
var activeList: List<Object> = data
while (true) {
val match = activeList.firstOrNull { it.id == id }
if (match != null) return match.innerObjects
activeList = activeList.flatMap { it.innerObjects ?: emptyList<Object>() }
if (!activeList.isNotEmpty())
return null
}
}
thanks for the answer, tried the first one but it doesn't find the Object by id. it only returns innerObjects of the last item in data no matter the id, and the second one doesn't compile as data is ArrayList and it doesn't have param id on line 4
– Rainmaker
Nov 22 '18 at 3:53
Finally had a chance to address this with a compiler in front of me, and have corrected my solutions. I would highly suggest not using the recursive search. And also, you're final solution and the recursive solution in general, are using nested loops, which are also less desirable.
– gtcompscientist
Nov 24 '18 at 19:23
add a comment |
So, in this case using basic recursion will solve this, but beware, because this kind of searching can quickly produce literal StackOverflowException
s.
fun getInnerObjectsById(nodes: ArrayList<Object>, id: Int) : ArrayList<Object>? {
nodes.forEach{
val ans = solve(it, id)
if (ans != null) return ans
}
return null
}
fun getInnerObjectsById(obj: Object?, id: Int): ArrayList<Object>? {
if (obj?.id == null || obj.innerObjects == null) return null
if (obj.id == id) return obj.innerObjects
return obj.innerObjects?.mapNotNull {
solve(it, id)
}?.firstOrNull()
}
This is a more involved, but imperative search.
fun getInnerObjectsById(data: ArrayList<Object>, id: Int): ArrayList<Object>? {
var activeList: List<Object> = data
while (true) {
val match = activeList.firstOrNull { it.id == id }
if (match != null) return match.innerObjects
activeList = activeList.flatMap { it.innerObjects ?: emptyList<Object>() }
if (!activeList.isNotEmpty())
return null
}
}
So, in this case using basic recursion will solve this, but beware, because this kind of searching can quickly produce literal StackOverflowException
s.
fun getInnerObjectsById(nodes: ArrayList<Object>, id: Int) : ArrayList<Object>? {
nodes.forEach{
val ans = solve(it, id)
if (ans != null) return ans
}
return null
}
fun getInnerObjectsById(obj: Object?, id: Int): ArrayList<Object>? {
if (obj?.id == null || obj.innerObjects == null) return null
if (obj.id == id) return obj.innerObjects
return obj.innerObjects?.mapNotNull {
solve(it, id)
}?.firstOrNull()
}
This is a more involved, but imperative search.
fun getInnerObjectsById(data: ArrayList<Object>, id: Int): ArrayList<Object>? {
var activeList: List<Object> = data
while (true) {
val match = activeList.firstOrNull { it.id == id }
if (match != null) return match.innerObjects
activeList = activeList.flatMap { it.innerObjects ?: emptyList<Object>() }
if (!activeList.isNotEmpty())
return null
}
}
edited Nov 24 '18 at 19:22
answered Nov 22 '18 at 3:11
gtcompscientistgtcompscientist
613516
613516
thanks for the answer, tried the first one but it doesn't find the Object by id. it only returns innerObjects of the last item in data no matter the id, and the second one doesn't compile as data is ArrayList and it doesn't have param id on line 4
– Rainmaker
Nov 22 '18 at 3:53
Finally had a chance to address this with a compiler in front of me, and have corrected my solutions. I would highly suggest not using the recursive search. And also, you're final solution and the recursive solution in general, are using nested loops, which are also less desirable.
– gtcompscientist
Nov 24 '18 at 19:23
add a comment |
thanks for the answer, tried the first one but it doesn't find the Object by id. it only returns innerObjects of the last item in data no matter the id, and the second one doesn't compile as data is ArrayList and it doesn't have param id on line 4
– Rainmaker
Nov 22 '18 at 3:53
Finally had a chance to address this with a compiler in front of me, and have corrected my solutions. I would highly suggest not using the recursive search. And also, you're final solution and the recursive solution in general, are using nested loops, which are also less desirable.
– gtcompscientist
Nov 24 '18 at 19:23
thanks for the answer, tried the first one but it doesn't find the Object by id. it only returns innerObjects of the last item in data no matter the id, and the second one doesn't compile as data is ArrayList and it doesn't have param id on line 4
– Rainmaker
Nov 22 '18 at 3:53
thanks for the answer, tried the first one but it doesn't find the Object by id. it only returns innerObjects of the last item in data no matter the id, and the second one doesn't compile as data is ArrayList and it doesn't have param id on line 4
– Rainmaker
Nov 22 '18 at 3:53
Finally had a chance to address this with a compiler in front of me, and have corrected my solutions. I would highly suggest not using the recursive search. And also, you're final solution and the recursive solution in general, are using nested loops, which are also less desirable.
– gtcompscientist
Nov 24 '18 at 19:23
Finally had a chance to address this with a compiler in front of me, and have corrected my solutions. I would highly suggest not using the recursive search. And also, you're final solution and the recursive solution in general, are using nested loops, which are also less desirable.
– gtcompscientist
Nov 24 '18 at 19:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422696%2fiterate-through-nested-list-to-return-inner-list-by-specified-param%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You need to use a graph traversal algorithm, such as depth-first search or breadth-first search.
– ruakh
Nov 22 '18 at 1:45
@ruakh looked into those but the problem I faced that my return breaks the recursion. In the examples I found the algorithms don't return specific Node. Could you provide an example with the return?
– Rainmaker
Nov 22 '18 at 2:23