Leetcode429.N-ary Tree Level Order TraversalN叉树的层序遍历
给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
例如,给定一个 3叉树 :
返回其层序遍历:
[ [1], [3,2,4], [5,6] ]
说明:
- 树的深度不会超过 1000。
- 树的节点总数不会超过 5000。
class Solution {
public:
vector<vector<int> > levelOrder(Node* root) {
vector<vector<int> > res;
if(root == NULL)
return res;
queue<Node*> q;
q.push(root);
while(!q.empty())
{
vector<int> temp;
int len = q.size();
for(int i = 0; i < len; i++)
{
Node* node = q.front();
q.pop();
temp.push_back(node ->val);
for(int j = 0; j < node ->children.size(); j++)
{
if(node ->children[j] != NULL)
q.push(node ->children[j]);
}
}
res.push_back(temp);
}
return res;
}
};
Leetcode429.N-ary Tree Level Order TraversalN叉树的层序遍历的更多相关文章
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- Binary Tree Level Order Traversal II(层序遍历2)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...
- [Leetcode] Binary tree level order traversal ii二叉树层次遍历
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
随机推荐
- HYNB Round 8: 2016 ICPC Amritapuri Regionals
HYNB Round 8: 2016 ICPC Amritapuri Regionals A - Tim and BSTs 做法 经典的树 DP 问题. \(dp[u][i]\) 表示考虑以 u 为根 ...
- Activiti业务键(businessKey)
问题:如何让业务对象和对应的流程 关联? 发现ProcessInstance 有个方法getBusinessKey()可以得到一个businessKey. ProcessInstance 对应数据库中 ...
- Luogu P4246 [SHOI2008]堵塞的交通(线段树+模拟)
P4246 [SHOI2008]堵塞的交通 题意 题目描述 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个\(2\)行\(C\)列的矩形 ...
- Python全栈开发:configparser模块
#!/usr/bin/env python # -*- coding;utf-8 -*- import configparser # 创建对象 conn = configparser.ConfigPa ...
- JS 标签页切换(复杂)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- github和gitlab仓库一起使用
github是网络公有代码仓库,一般用于私人代码托管,而gitlab一般是企业搭建的内部代码仓库.工作期间,我们都会同时用到这两个仓库.可公司邮箱与个人邮箱是不同的,由此产生的 SSH key 也是不 ...
- Django之模板语言(二)-----Filter
1.其他常用的模板语言: 通过模板语言可以让前端页面显示数据,数据可以是基本数据类型,也可以是对象亦或者对象的列表,结合着模板中的for.if等配合使用. 要注意前端页面中,出现没有后端数据的情况,随 ...
- ORA-01790: 表达式必须具有与对应表达式相同的数据类型
出现这种错误,要先看一下是不是sql中有用到连接:union,unio all之类的,如果有,需要注意相同名称字段的数据类型一定要相同.
- #iOS问题记录# 频繁触发viewDidLayoutSubviews的问题
问题描述: 最近使用给Flutter团队写view组件的时候,出现了触发Widget的频繁build的问题. 问题排查: Flutter的同事提到在flutter层,是因为 updateViewpor ...
- Nginx报错汇总
1. Nginx 无法启动解决方法 在查看到 logs 中报了如下错误时: 0.0.0.0:80 failed (10013: An attempt was made to access a ...