LeetCode 107 Binary Tree Level Order Traversal II
2015-06-13 14:35
309 查看
题目
分析
同题102 Leetcode 102 层序遍历到底只是vector插入的顺序不同
题解
/** * 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>> levelOrderBottom(TreeNode* root) { vector<vector<int>> value; if(root == NULL) return value; vector<TreeNode*> temp; temp.push_back(root); while(!temp.empty()){ vector<int> vec; vector<TreeNode*> tree; for(int i=0;i<temp.size();i++){ vec.push_back(temp[i]->val); if(temp[i]->left != NULL) tree.push_back(temp[i]->left); if(temp[i]->right != NULL) tree.push_back(temp[i]->right); } value.insert(value.begin(),vec); temp = tree; } return value; } };
相关文章推荐
- Shiro的helloword
- linux下串口的阻塞和非阻塞操作
- stm32库函数学习篇---NVIC与外部中断
- 第14周 【项目2-用文件保存的学生名单】
- 预测
- objective-c集合类
- 第一节:程序集加载
- Linux 高可用(HA)集群之Heartbeat详解
- Android StateMachine解析( 1 )
- HashMap的数据结构
- Sprite Kit Swift游戏开发新手指导手册(5)
- 【bzoj_1043】[HAOI2008]下落的圆盘
- 使用Java完成《算法导论》习题2.3-6
- IntelliJ IDEA导航特性Top20
- TOMCAT 关闭报错:Tomcat did not stop in time. PID file was not removed
- 设计模式(解释器模式)
- ios App 界面截取成图片
- 5种服务器网络编程模型讲解
- win7 安装 wamp2.5版本的memcache+memcached
- 技术Site