429. N-ary Tree Level Order Traversal(easy)

Easy

32635FavoriteShare

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:

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

 

BFS法:

C++:

/*
 * @Autor: SourDumplings
 * @Date: 2019-09-25 09:42:58
 * @Link: https://github.com/SourDumplings/
 * @Email: changzheng300@foxmail.com
 * @Description: https://leetcode.com/problems/n-ary-tree-level-order-traversal/
 */

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;
    Node() {}
    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution
{
public:
    vector<vector<int>> levelOrder(Node *root)
    {
        vector<vector<int>> res;
        if (!root)
        {
            return res;
        }
        doLevelOrder(res, root);
        return res;
    }

    void doLevelOrder(vector<vector<int>> &res, Node *root)
    {
        queue<Node *> thisQ;
        thisQ.push(root);
        while (!thisQ.empty())
        {
            vector<int> thisLevel;
            queue<Node *> nextQ;
            while (!thisQ.empty())
            {
                Node *p = thisQ.front();
                thisQ.pop();
                thisLevel.push_back(p->val);
                for (Node *c : p->children)
                {
                    nextQ.push(c);
                }
            }
            thisQ = std::move(nextQ);
            res.push_back(thisLevel);
        }
    }
};

 Java:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/*
 * @Autor: SourDumplings
 * @Date: 2019-09-25 11:10:59
 * @Link: https://github.com/SourDumplings/
 * @Email: changzheng300@foxmail.com
 * @Description: https://leetcode.com/problems/n-ary-tree-level-order-traversal/
 */

/*
// 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 List<List<Integer>> levelOrder(Node root)
    {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if (root == null)
        {
            return res;
        }
        doLevelOrder(res, root);
        return res;
    }

    public void doLevelOrder(List<List<Integer>> res, Node root)
    {
        Queue<Node> thisQ = new LinkedList<>();
        thisQ.add(root);
        while (!thisQ.isEmpty())
        {
            Queue<Node> nextQ = new LinkedList<>();
            List<Integer> thisLevel = new ArrayList<>();
            while (!thisQ.isEmpty())
            {
                Node p = thisQ.poll();
                thisLevel.add(p.val);
                for (Node c : p.children)
                {
                    nextQ.add(c);
                }
            }

            res.add(thisLevel);
            thisQ = nextQ;
        }
    }
}

DFS法:

Java:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/*
 * @Autor: SourDumplings
 * @Date: 2019-09-25 11:27:52
 * @Link: https://github.com/SourDumplings/
 * @Email: changzheng300@foxmail.com
 * @Description: https://leetcode.com/problems/n-ary-tree-level-order-traversal/
 */

/*
// 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 List<List<Integer>> levelOrder(Node root)
    {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if (root == null)
        {
            return res;
        }
        doLevelOrder(res, root, 1);
        return res;
    }

    public void doLevelOrder(List<List<Integer>> res, Node root, int level)
    {
        if (res.size() < level)
        {
            res.add(new ArrayList<Integer>());
        }
        res.get(level - 1).add(root.val);
        for (Node c : root.children)
        {
            doLevelOrder(res, c, level + 1);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值