国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 559. Maximum Depth of N-ary Tree

EdwardUp / 768人閱讀

Problem

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.

For example, given a 3-ary tree:

We should return its max depth, which is 3.

Note:

The depth of the tree is at most 1000.
The total number of nodes is at most 5000.

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

    public Node() {}

    public Node(int _val,List _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public int maxDepth(Node root) {
        return helper(root);
    }
    private int helper(Node root) {
        if (root == null) return 0;
        int maxDepth = 1;
        for (Node node: root.children) {
            maxDepth = Math.max(maxDepth, 1+helper(node));
        }
        return maxDepth;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/77378.html

相關文章

  • Leetcode PHP題解--D42 559. Maximum Depth of N-ary Tr

    摘要:題目鏈接題目分析此題和上一題思路一樣。只是不是二叉樹。思路略最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D42 559. Maximum Depth of N-ary Tree 題目鏈接 559. Maximum Depth of N-ary Tree 題目分析 此題和上一題思路一樣。只是不是二叉樹。而是正常的樹。 思路 略 最終代碼

    CrazyCodes 評論0 收藏0
  • [LeetCode] 429. N-ary Tree Level Order Traversal (

    429. N-ary Tree Level Order Traversal Given an n-ary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level). For example, given a 3-ary tree:showImg(https...

    LiangJ 評論0 收藏0
  • leetcode429. N-ary Tree Level Order Traversal

    摘要:題目要求對叉樹進行水平遍歷,并輸出每一行遍歷的結果。因此無需再用隊列來額外存儲每一行的水平遍歷,可以直接通過遞歸將遍歷結果插入到相應行的結果集中。 題目要求 Given an n-ary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level)...

    tomlingtm 評論0 收藏0
  • 前端 | 每天一個 LeetCode

    摘要:在線網站地址我的微信公眾號完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個題。這是項目地址歡迎一起交流學習。 這篇文章記錄我練習的 LeetCode 題目,語言 JavaScript。 在線網站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號: showImg(htt...

    張漢慶 評論0 收藏0
  • LeetCode 104 Maximum Depth of Binary Tree 二叉樹最大深度

    LeetCode 104 Maximum Depth of Binary Tree難度:Easy 題目描述:找到一顆二叉樹的最深深度。Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down ...

    PiscesYE 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<