LeetCode 107:Binary Tree Level Order Traversal II
2015-12-15 12:27
489 查看
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree
return its bottom-up level order traversal as:
confused what
read more on how binary tree is serialized on OJ.
Subscribe to
see which companies asked this question
For example:
Given binary tree
{3,9,20,#,#,15,7},
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
confused what
"{1,#,2,3}"means? >
read more on how binary tree is serialized on OJ.
Subscribe to
see which companies asked this question
//解题思路:分层遍历二叉树,用队列模拟。 class Solution { public: vector<vector<int> > levelOrderBottom(TreeNode *root) { vector< vector<int> > result; if (root == NULL) return result; queue<TreeNode*> q; q.push(root); int count = 1; //count表示当前层的节点个数 int level = 0; vector<int> tmp(0); //tmp用来临时保存当前层的节点 while (!q.empty()) { tmp.clear(); level = 0; for (int i = 0; i < count; ++i) { root = q.front(); q.pop(); tmp.push_back(root->val); if (root->left != NULL) { q.push(root->left); //队列q用来保存下一层的节点 ++level; } if (root->right != NULL) { q.push(root->right); ++level; } } count = level; result.push_back(tmp); } reverse(result.begin(), result.end()); return result; } };
相关文章推荐
- 2015 ACM-ICPC 上海 EC-Final 总结
- 无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件。你想要在安装 VMware Workstation 前重启吗?
- String.Format格式说明
- js中var
- 面试题总结(2015-12)
- 友盟微信登录-切换账号重新拉起授权
- HDOJ--2003
- 文字垂直居中,水平居中 a标签水平居中只要给他的父级设置text-align=center
- c语言: Standard C 语言标准函数库
- LeetCode "Binary Tree Vertical Order"
- R树空间索引
- Delphi属性Property学习应用总结
- Impala与Hbase整合用于ETL过程尝试(1)
- Android里面主菜单里面设置菜单列表,单项选择,多项选择的实现
- 采用大杀招QEMU调试Linux内核代码
- 服务器相关名词解释
- 300. Longest Increasing Subsequence
- C#下载文件(TransmitFile/WriteFile/流方式)实例介绍
- HDOJ--2002
- win32记事本程序(一)