-
[LeetCode] 559. Maximum Depth of N-ary Tree c++Coding Test/LeetCode 2022. 12. 20. 17:55728x90
- 문제
Given a n-ary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] Output: 5
트리의 depth 중 가장 큰 값을 구하는 문제이다.
- 문제 해결
int maxDepth(Node* root) { if(root==NULL) return 0; int max_depth = 1; for(int i=0;i<root->children.size();i++){ max_depth = max(max_depth, 1 + maxDepth(root->children[i])); } return max_depth; }
728x90'Coding Test > LeetCode' 카테고리의 다른 글
[LeetCode] 965. Univalued Binary Tree c++ (0) 2022.12.22 [LeetCode] 463. Island Perimeter c++ (0) 2022.12.22 [LeetCode] 637. Average of Levels in Binary Tree c++ (0) 2022.12.20 [LeetCode] 104. Maximum Depth of Binary Tree c++ (1) 2022.12.20 [LeetCode] 94. Binary Tree Inorder Traversal c++ (0) 2022.12.19