package com.java.practice.basic; import com.google.gson.Gson; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; public class LevelOrderTraversal { public static void main(String[] args) { Map testData = getTestData(); List> testStempList = (List>) testData.get("testSteps"); List stepList = Arrays.asList("67922544a617056f7435efb6"); // find the solution(testStempList, stepList, 0); } private static void solution(List> testStempList, List stepList, int index) { String pathStepId = stepList.get(index); for (Map step : testStempList) { String stepId = step.getOrDefault("id", "").toString(); if (pathStepId.equals(stepId)) { // iterate through all children with next pathStepId System.out.println(step.get("id")); List> childStepList = (List>) step.getOrDefault("testSteps", new ArrayList<>()); if (index + 1 == stepList.size()) { // need to directly add all children for (Map stringObjectMap : childStepList) { System.out.println(stringObjectMap.get("id")); } } else { // traverse through all children solution(childStepList, stepList, index + 1); } return; } else { // Need to take it System.out.println(step.get("id")); } } } static Map getTestData() { return (Map) new Gson().fromJson("{\n \"id\": \"6790730c94077efcdef80990\",\n \"name\": \"Test Scenario 678e2c3132c98079bc8baa8f\",\n \"testSteps\": [\n {\n \"id\": \"678e2c5732c98079bc8ba111\",\n \"action\": \"\",\n \"name\": \"step1\",\n \"testSteps\": [\n {\n \"id\": \"678e36405393795b99e86011\",\n \"action\": \"\",\n \"name\": \"step1.1\",\n \"testSteps\": [\n {\n \"id\": \"678e36405393795b99e81111\",\n \"action\": \"\",\n \"name\": \"step1.1.1\",\n \"testSteps\": [\n {\n \"id\": \"678e36405393795b99e22222\",\n \"action\": \"\",\n \"name\": \"step1.1.1 Child\",\n \"testSteps\": [\n {\n \"id\": \"678e36405393795b99e33333\",\n \"action\": \"\",\n \"name\": \"step1.1.1 Child 2222\"\n }\n ]\n }\n ]\n },\n {\n \"id\": \"678e36405393795b99e81112\",\n \"action\": \"\",\n \"name\": \"step1.1.2\"\n }\n ]\n },\n {\n \"id\": \"678e36405393795b99e86012\",\n \"action\": \"\",\n \"name\": \"step1.2\",\n \"testSteps\": [\n {\n \"id\": \"678e36405393795b99e86112\",\n \"action\": \"\",\n \"name\": \"step1.2\"\n }\n ]\n }\n ]\n },\n {\n \"id\": \"678e2c5732c98079bc8ba222\",\n \"name\": \"step2\",\n \"version\": 1,\n \"continueAfterFail\": false,\n \"testBlockId\": \"7cdsy8cs7dcdhsc\",\n \"useManualElementSelection\": false,\n \"executeBeforeAiStep\": false,\n \"elements\": [],\n \"createdBy\": 0,\n \"updatedBy\": 0,\n \"disabled\": false,\n \"stepHasPreCondition\": false\n },\n {\n \"id\": \"67922544a617056f7435efb6\",\n \"action\": \"\",\n \"name\": \"step3\",\n \"version\": 1,\n \"text\": \"\",\n \"continueAfterFail\": false,\n \"useManualElementSelection\": false,\n \"executeBeforeAiStep\": false,\n \"testSteps\": [],\n \"createdAt\": 1737631044915,\n \"updatedAt\": 1737631044915,\n \"createdBy\": 307,\n \"updatedBy\": 307,\n \"disabled\": false,\n \"stepHasPreCondition\": false\n }\n ]\n}\n", Map.class); } }