首页 > 其他 > 详细

559. Maximum Depth of N-ary Tree - LeetCode

时间:2018-08-31 19:56:51      阅读:165      评论:0      收藏:0      [点我收藏+]

Question

559. Maximum Depth of N-ary Tree

技术分享图片

Solution

题目大意:N叉树求最大深度

思路:用递归做,树的深度 = 1 + 子树最大深度

Java实现:

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val,List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
    public int maxDepth(Node root) {
        if(root == null) return 0;
        int depth = 0;
        for (Node child : root.children) {
            depth = Math.max(depth, maxDepth(child));
        }
        return depth + 1;
    }
}

559. Maximum Depth of N-ary Tree - LeetCode

原文:https://www.cnblogs.com/okokabcd/p/9567359.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!