题目

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代码

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<vector<int>> levelOrder(TreeNode* root) {
  13. //层次遍历,分层存储
  14. if (!root)
  15. return vector<vector<int> >();
  16. vector<vector<int> > ret;
  17. //定义两个队列,一个存储所有的父节点
  18. queue<TreeNode *> parents;
  19. parents.push(root);
  20. while (!parents.empty())
  21. {
  22. //存储当前层的遍历结果
  23. vector<int> tmp;
  24. //定义队列另一个存储他们的子节点也就是子层
  25. queue<TreeNode *> childs;
  26. while (!parents.empty())
  27. {
  28. TreeNode *node = parents.front();
  29. tmp.push_back(node->val);
  30. //弹出当前父节点
  31. parents.pop();
  32. if (node->left)
  33. childs.push(node->left);
  34. if (node->right)
  35. childs.push(node->right);
  36. }
  37. //存储当前层的遍历结果
  38. ret.push_back(tmp);
  39. //遍历下一层
  40. parents = childs;
  41. }
  42. return ret;
  43. }
  44. };

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. Ubuntu设置右键打开终端

    1:设置Ubuntu右键打开终端. Ctrl+Alt+T 打开终端 $ sudo apt-get ins tall nautilus-open-terminal 重启系统 2:进入root用户认证失败 ...

  2. Jasper_crosstab_measure_display a value of field in crosstab total row

    1.create a measure <measure name="myField" class="java.lang.String"> <m ...

  3. python 生成器与迭代器

    #! /usr/bin/env python# -*- coding:utf-8 -*- def xrange(n): num = 0 while True: if num > n: retur ...

  4. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  5. .Net 遍历目录下第一层的子文件夹和子文件夹里的文件

    今天再完成一道任务的时候需要遍历得到所有txt文件,搜索很久终于得到了一个很方便的方法. foreach (string o in Directory.GetDirectories(@"D: ...

  6. Windows服务的新建,安装,卸载,调试以及调用!

    一.前言: 写这篇博文之前,我正顶着压力在尝试着调试我一无所知的Windows自建服务.历经千辛万苦,找了无数零散文档拼凑关于VisualStudio2015中怎样创建和调试服务程序!最后终于调试成功 ...

  7. TLS、SSL、HTTPS以及证书

    转自:http://www.cnblogs.com/kyrios/p/tls-and-certificates.html 最近在研究基于ssl的传输加密,涉及到了key和证书相关的话题,走了不少弯路, ...

  8. 【NumPy学习指南】day4 多维数组的切片和索引

    ndarray支持在多维数组上的切片操作.为了方便起见,我们可以用一个省略号(...)来 表示遍历剩下的维度. (1) 举例来说,我们先用arange函数创建一个数组并改变其维度,使之变成一个三维数组 ...

  9. java.lang.IllegalAccessException: Class XX can not access a member of class XXX with modifiers "private static"

    当前需求: 利用反射获取某一属性值运行结果:java.lang.IllegalAccessException: Class com.example.demo.test.Reflect can not ...

  10. 洛谷 P2922 [USACO08DEC]秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...