题目:Binay 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 {,,,#,#,,},

如下一棵树

转换之后需要输出这样的形式:

如下,见代码:

 struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode():val(),left(NULL),right(NULL) {}
TreeNode(int x): val(x), left(NULL),right(NULL) {}
}; vector<vector<int> > levelOrderTraversal(TreeNode *root) //非递归的中序遍历(用栈实现)
{ queue<TreeNode *> tree_queue;
vector<vector<int> > tree_vector;
vector<int> svector; if (NULL == root) {
return tree_vector;
}
TreeNode *pTemp = root;
tree_queue.push(root);
tree_queue.push(NULL); //the end of one level. while (true) {
TreeNode *pTemp = tree_queue.front();
tree_queue.pop(); if (!pTemp) { //get the null, put vector<> to vector<vector<>>
tree_vector.push_back(svector);
svector.clear();
if (tree_queue.empty())
break;
tree_queue.push(NULL);
}
else {
svector.push_back(pTemp->val);
if (pTemp->left)
tree_queue.push(pTemp->left);
if (pTemp->right)
tree_queue.push(pTemp->right);
}
}
return tree_vector;
}

LeetCode: 102_Binary Tree Level Order Traversal | 二叉树自顶向下的层次遍历 | Easy的更多相关文章

  1. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

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

  2. [Leetcode] Binary tree Zigzag level order traversal二叉树Z形层次遍历

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

  3. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  4. [Leetcode] Binary tree level order traversal二叉树层次遍历

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

  5. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  6. [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 ...

  7. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

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

  8. [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 ...

  9. [LeetCode] N-ary Tree Level Order Traversal N叉树层序遍历

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. 教女朋友写第一个php

    1 首先 下载xampp 软件.按默认安装好之后 在电脑的右下角的箭头里能发现一个橘色的小图标 双击它 启动阿帕奇和mysql 会变绿 2 打开 C:\xampp\apache 文件夹 找到httpd ...

  2. Windbg断点调试

    [文章主题] Windbg是Windows驱动调试的重要软件,也是必须学习的软件,前面的博客介绍了一些双机调试的环境配置,只要按照我所说的步骤一步步下来就可以完成环境搭建. 本文主要介绍如何调试sys ...

  3. uvm_pre_do

    https://blog.csdn.net/tingtang13/article/details/46535649 1.uvm_do 封装了一系列接口,封装越多,灵活性越差.所以增加了三个接口:pre ...

  4. 【python深入】collections-Counter使用总结

    关于collections的使用,首先介绍:Counter的使用 需要执行:from collections import Counter 在很多使用到dict和次数的场景下,Python中用Coun ...

  5. 220. Contains Duplicate III 数组指针差k数值差t

    [抄题]: Given an array of integers, find out whether there are two distinct indices i and j in the arr ...

  6. 337. House Robber III二叉树上的抢劫题

    [抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  7. [leetcode]10. Regular Expression Matching正则表达式的匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  8. Spring Kafka中关于Kafka的配置参数

    #################consumer的配置参数(开始)################# #如果'enable.auto.commit'为true,则消费者偏移自动提交给Kafka的频率 ...

  9. webpack浅析---出口篇

    webpack有四个核心概念: 入口(entry) 输出(output) loader 插件(plugins) 输出: 在哪里输出创建的bundles,以及如何命名这些文件, 默认./dist fil ...

  10. springboot与缓存(redis,或者caffeine,guava)

    1.理论介绍 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. CachingProvide ...