LeetCode: 102_Binary Tree Level Order Traversal | 二叉树自顶向下的层次遍历 | Easy
题目: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的更多相关文章
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [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 ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [Leetcode] Binary tree level order traversal二叉树层次遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- [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] 102. Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [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] 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, ...
随机推荐
- 教女朋友写第一个php
1 首先 下载xampp 软件.按默认安装好之后 在电脑的右下角的箭头里能发现一个橘色的小图标 双击它 启动阿帕奇和mysql 会变绿 2 打开 C:\xampp\apache 文件夹 找到httpd ...
- Windbg断点调试
[文章主题] Windbg是Windows驱动调试的重要软件,也是必须学习的软件,前面的博客介绍了一些双机调试的环境配置,只要按照我所说的步骤一步步下来就可以完成环境搭建. 本文主要介绍如何调试sys ...
- uvm_pre_do
https://blog.csdn.net/tingtang13/article/details/46535649 1.uvm_do 封装了一系列接口,封装越多,灵活性越差.所以增加了三个接口:pre ...
- 【python深入】collections-Counter使用总结
关于collections的使用,首先介绍:Counter的使用 需要执行:from collections import Counter 在很多使用到dict和次数的场景下,Python中用Coun ...
- 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 ...
- 337. House Robber III二叉树上的抢劫题
[抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...
- [leetcode]10. Regular Expression Matching正则表达式的匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- Spring Kafka中关于Kafka的配置参数
#################consumer的配置参数(开始)################# #如果'enable.auto.commit'为true,则消费者偏移自动提交给Kafka的频率 ...
- webpack浅析---出口篇
webpack有四个核心概念: 入口(entry) 输出(output) loader 插件(plugins) 输出: 在哪里输出创建的bundles,以及如何命名这些文件, 默认./dist fil ...
- springboot与缓存(redis,或者caffeine,guava)
1.理论介绍 Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry. CachingProvide ...