-
559. Maximum Depth of N-ary TreeAlgorithm/java tip 2021. 3. 6. 21:48
leetcode.com/problems/maximum-depth-of-n-ary-tree/
class Solution { public int maxDepth(Node root) { if(root == null) return 0; Queue<Node> q = new LinkedList<>(); q.offer(root); int depth = 0; while(!q.isEmpty()) { depth++; int size = q.size(); for(int i=0; i<size; i++) { Node node = q.poll(); for(Node child : node.children) { q.offer(child); } } } return depth; } /* public int maxDepth(Node root) { if (root == null) return 0; int max = 0; for(Node n : root.children) { int temp = maxDepth(n); if(temp > max) max = temp; } return max + 1; } */ }
'Algorithm > java tip' 카테고리의 다른 글
404. Sum of Left Leaves (0) 2021.03.06 22. Generate Parentheses (0) 2021.03.06 [programmers] 가장 먼 노드 (0) 2021.03.06 [programmers] 큰 수 만들기 (0) 2021.03.06 [programmers] 체육복 (0) 2021.03.06