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

資訊專欄INFORMATION COLUMN

[LeetCode] 429. N-ary Tree Level Order Traversal (

LiangJ / 2612人閱讀

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:

We should return its level order traversal:

[
     [1],
     [3,2,4],
     [5,6]
]    

Note:

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

Solution (Iteration)

use Queue, in each level, use queue.size() to loop

class Solution {
    public List> levelOrder(Node root) {
        List> res = new ArrayList<>();
        if (root == null) return res;
        Queue queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List curRes = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                Node node = queue.poll();
                curRes.add(node.val);
                for (Node child: node.children) {
                    queue.offer(child);
                }
            }
            res.add(curRes);
        }
        return res;
    }
}
102. Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes" values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / 
  9  20
    /  
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]
Solution (Iteration)
class Solution {
    public List> levelOrder(TreeNode root) {
        List> res = new ArrayList<>();
        if (root == null) return res;
        Queue queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List curRes = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                curRes.add(node.val);
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            res.add(curRes);
        }
        return res;
    }
}

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

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

相關文章

  • 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 PHP題解--D55 429. N-ary Tree Level Order Tr

    摘要:題目鏈接題目分析按層遍歷叉樹。思路以層數為鍵,塞入當前節點的值。最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D55 429. N-ary Tree Level Order Traversal 題目鏈接 429. N-ary Tree Level Order Traversal 題目分析 按層遍歷N叉樹。 思路 以層數為鍵,塞入當前節點的值。 遞歸遍歷即可。 最終代碼

    libxd 評論0 收藏0
  • [LeetCode] 590. N-ary Tree Postorder Traversal (vs

    摘要:按順序放入,正好方面是從到,順序方面是從最右到最左,因為是先入后出。這樣最后一下就是先左后右,先子后根。 590. N-ary Tree Postorder Traversal Problem Given an n-ary tree, return the postorder traversal of its nodes values.For example, given a 3-ar...

    sydMobile 評論0 收藏0
  • [LeetCode] 589. N-ary Tree Preorder Traversal (vs.

    589. N-ary Tree Preorder Traversal Given an n-ary tree, return the preorder traversal of its nodes values.For example, given a 3-ary tree:showImg(https://segmentfault.com/img/bVbhKkv?w=781&h=502);Retu...

    array_huang 評論0 收藏0
  • Leetcode PHP題解--D43 589. N-ary Tree Preorder Trave

    摘要:題目鏈接題目分析維數組的先序遍歷。這題也不想多說什么了。是比較基礎的題目了。先序就是先根后子而已。思路在遍歷子節點之前,先保存當前節點的信息。最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D43 589. N-ary Tree Preorder Traversal 題目鏈接 589. N-ary Tree Preorder Traversal 題目分析 N維數組的先序遍歷。 這題也...

    junbaor 評論0 收藏0

發表評論

0條評論

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