码迷,mamicode.com
首页 > 其他好文 > 详细

559. Maximum Depth of N-ary Tree

时间:2020-06-12 14:27:25      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:lex   -o   linked   turn   examples   rom   while   res   import   

package LeetCode_559

import java.util.*

/**
 * 559. Maximum Depth of N-ary Tree
 * https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/
 *
 * 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).
 * */
class Node(var `val`: Int) {
    var children: List<Node?> = listOf()
}

class Solution {
    /**
     * solution: BFS, Time complexity:O(n), Space complexity:O(n)
     * */
    fun maxDepth(root: Node?): Int {
        if (root == null) {
            return 0
        }
        var res = 0
        val queue = LinkedList<Node>()
        queue.offer(root)
        while (queue.isNotEmpty()){
            res++
            val size = queue.size
            for (i in size-1 downTo 0){
                val cur = queue.poll()
                if (cur!=null){
                    for (item in cur.children){
                        queue.offer(item)
                    }
                }
            }
        }
        return res
    }
}

 

559. Maximum Depth of N-ary Tree

标签:lex   -o   linked   turn   examples   rom   while   res   import   

原文地址:https://www.cnblogs.com/johnnyzhao/p/13098922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!