题目

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,#,#,15,7}



return its level order traversal as:



confused what “{1,#,2,3}” means? > read more on how binary tree is serialized on OJ.

OJ’s Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where ‘#’ signifies a path terminator where no node exists below.

Here’s an example:



The above binary tree is serialized as “{1,2,3,#,#,4,#,#,5}”.

分析

给定一颗二叉树,返回其层序遍历序列。

要求按照二叉树的结构,分层存储节点元素。

既然要求分层返回遍历结果,我们知道,层序遍历是利用队列先进先出的结构特点,依次遍历。按照本题要求,在遍历过程中必须将每层涉及到的节点单独存储,才能保证得到独立层节点序列。

//定义两个队列,一个存储所有的父节点,另一个存储他们的子节点也就是子层

AC代码

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
//层次遍历,分层存储
if (!root)
return vector<vector<int> >(); vector<vector<int> > ret; //定义两个队列,一个存储所有的父节点
queue<TreeNode *> parents; parents.push(root); while (!parents.empty())
{
//存储当前层的遍历结果
vector<int> tmp;
//定义队列另一个存储他们的子节点也就是子层
queue<TreeNode *> childs;
while (!parents.empty())
{
TreeNode *node = parents.front();
tmp.push_back(node->val);
//弹出当前父节点
parents.pop(); if (node->left)
childs.push(node->left); if (node->right)
childs.push(node->right);
}
//存储当前层的遍历结果
ret.push_back(tmp); //遍历下一层
parents = childs;
}
return ret;
}
};

GitHub测试程序源码

LeetCode(102) Binary Tree Level Order Traversal的更多相关文章

  1. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  2. LeetCode(26)-Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  3. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

  4. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  5. [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  6. 【leetcode❤python】107. Binary Tree Level Order Traversal II

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  7. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  8. LeetCode(103) Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  9. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

随机推荐

  1. Mysql的跨服务器操作

    1.查询FEDERATED功能是否开启: show ENGINES; 2.如果状态为NO则需修改my.ini文件,增加一行federated配置: my.ini配置文件的默认路径 C:\Program ...

  2. Java程序流程控制之if-else if-else

    java基础之流程控制(一)    流程控制 Flow Control :       流程控制语句是编程语言中的核心之一.可以分为 分支语句.循环语句和跳转语句.        本讲内容包括分支语句 ...

  3. 一本通 1434:【例题2】Best Cow Fences

    Best Cow Fences 二分答案 + 前缀和 个人认为题意没有表述清楚,本题要求的是满足题意的连续子序列(难度大大降低了有木有). 本题的精度也是非常令人陶醉,请您自行体会吧! #includ ...

  4. NOIP数学相关模板整理

    $O(n)$递推求逆元 #include<cstdio> #include<cstring> #include<algorithm> using namespace ...

  5. 2189 数字三角形W

    2189 数字三角形W 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold       题目描述 Description 数字三角形要求走到最后mod 100最大 输入描述 ...

  6. jmeter中文件上传配置

  7. POJ 2152 Fire (树形DP,经典)

    题意:给定一棵n个节点的树,要在某些点上建设消防站,使得所有点都能够通过某个消防站解决消防问题,但是每个点的建站费用不同,能够保证该点安全的消防站的距离上限也不同.给定每个点的建站费用以及最远的消防站 ...

  8. Invalid bound statement (not found): com.ros.dao.LogMapper.insert

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ros.dao.LogMapp ...

  9. adhoc无法下载应用程序 此时无法安装-解决

    解决方法 点击xcode,进入build setting,选择code signing,provisioning profile选择automatic 或者选择adhoc的provisioning p ...

  10. 单调栈3_水到极致的题 HDOJ4252

    A Famous City 题目大意 给出正视图  每一列为楼的高度 最少有几座楼 坑点 楼高度可以为0 代表没有楼 贡献了两发RE 原因 if(!s.empty()&&tem){s. ...