102 Binary Tree Level Order Traversal
2015-11-14 20:39
363 查看
/** * 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) { vector<vector<int>> res; if(root==NULL) return res; vector<int> r; queue<TreeNode*> tmp; tmp.push(root); queue<TreeNode*> tmp1; while(!tmp.empty()) { r.clear(); while(!tmp.empty()) { TreeNode* hehe = tmp.front(); r.push_back(hehe->val); if(hehe->left!=NULL) tmp1.push(hehe->left); if(hehe->right!=NULL) tmp1.push(hehe->right); tmp.pop(); } tmp = tmp1; while(!tmp1.empty()) tmp1.pop(); res.push_back(r); } return res; } };
相关文章推荐
- html 内联元素 块元素
- hdu3394Railway【双连通分量+模板详细解释】
- 决策树算法
- 20151114 shell脚本控制oracle随系统启动
- 112 Path Sum
- 10010---JavaWeb基础--HttpServlet
- Linux下的GPT分区
- 225 Implement Stack using Queues
- 动态页面jsp转换为html静态的网页
- 今天来学习下美团推荐算法实践:机器学习重排序模型
- pip安装mysql-python报致命错误:my_config.h:没有那个文件或目录
- 多态
- 172 Factorial Trailing Zeroes
- 118 Pascal's Triangle
- 运行地址和加载地址
- 第126讲:Hadoop集群管理之Datanode目录元数据结构详解学习笔记
- Ubuntu上安装谷歌第二代机器学习系统TensorFlow
- 66 Plus One
- IOS iPhone 开发中的文件读写及数据存储(一)
- 198 House Robber