Super Easy || 🚀Explained with Image🚀|| DFS || Java😊
Explaination for Approach :\n\nLet\u2019s consider we are at a node, say p, we will collect all apples \nin p\u2019s subtree before returning back to the origin
# Explaination for Approach :\n```\nLet\u2019s consider we are at a node, say p, we will collect all apples \nin p\u2019s subtree before returning back to the original root. This \nwill avoid traveling the same path multiple times.\n\nSay, the root is where we start, p is a node in the tree and p has \ntwo children \u2013 child1, child2 \u2013 and both of them have an apple each.\n\nSo the path we need to follow is :\nroot \u2013> p \u2013> child1 \u2013> p \u2013> child2 \u2013> p \u2014> root\n```\n![apple-leet.jpg](https://assets.leetcode.com/users/images/00d2e5ba-a858-4e4d-aed1-cd5f25140058_1673413186.164347.jpeg)\n\n```\nThus, seeing the above pattern we can infer that it\u2019s a simple DFS \ntraversal but we need to add the cost of traversal two times for \nany edge e because we also need to come back to it after collecting \nthe apples.\n```\n# Request \uD83D\uDE0A :\n```\nIf you find this solution easy to understand and helpful, then\nPlease Upvote\uD83D\uDC4D\uD83D\uDC4D.\n```\n\n# Code (with Explained Comments) :\n```\nclass Solution {\n    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {\n        \n        Map<Integer, List<Integer>> graph = createGraph(edges); // to store the graph\n        Map<Integer, Boolean> visited = new HashMap<>();\n    \n        return dfs(graph, 0, hasApple, 0, visited); // cost of reaching the root is 0. For all others, its 2.\n      \n    }\n    \n    private int dfs(Map<Integer, List<Integer>> graph, int node, List<Boolean> hasApple, int myCost, Map<Integer, Boolean> visited) {\n        Boolean v = visited.getOrDefault(node, false);\n    if (v) {\n      return 0;\n    }\n    visited.put(node, true);\n    \n        int childrenCost = 0; // cost of traversing all children. \n      \n        for(int n : graph.getOrDefault(node, new ArrayList<>())) {\n            childrenCost += dfs(graph, n, hasApple, 2, visited); // check recursively for all apples in subtrees.\n        }\n      \n        if (childrenCost == 0 && hasApple.get(node) == false) {\n          // If no child has apples, then we won\'t traverse the subtree, so cost will be zero.\n          // similarly, if current node also does not have the apple, we won\'t traverse this branch at all, so cost will be zero.\n          return 0;\n        }\n      \n        return childrenCost + myCost;\n    }\n    \n    private Map<Integer, List<Integer>> createGraph(int[][] edges) {\n        Map<Integer, List<Integer>> graph = new HashMap<>();\n      \n        for(int i = 0; i < edges.length; i++) {\n            List<Integer> list = graph.getOrDefault(edges[i][0], new ArrayList<>()); // Adjecency list representation.\n            list.add(edges[i][1]);\n            graph.put(edges[i][0], list);\n      \n      list = graph.getOrDefault(edges[i][1], new ArrayList<>()); // Adjecency list representation.\n            list.add(edges[i][0]);\n            graph.put(edges[i][1], list);\n        }\n      \n        return graph;\n    }\n}\n```
['Hash Table', 'Tree', 'Depth-First Search', 'Java']