LeetCode 559. Maximum Depth of N-ary Tree

using recursion to solve this problem:

maxDepth(root) = Math.max(maxDepth(root.kid1), Math.max(root.kid2)…) + 1;

class Solution {
    
    public int maxDepth(Node root) {
        if (root == null) return 0;
        int height = 0;
        for (Node child: root.children) {
            height = Math.max(height, maxDepth(child));
        }
        return height + 1;
    }
    
    
}
posted @ 2020-11-13 03:22  EvanMeetTheWorld  阅读(15)  评论(0)    收藏  举报