You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.7 KiB

2 years ago
/*/*******************************************************************************
** **
** Jiedi(China nanjing)Ltd. **
** **
*******************************************************************************/
/*****************************FILE INFOMATION***********************************
**
** Project : ---leetcode
** Contact : xiacaojun@qq.com
** : http://blog.csdn.net/jiedichina
** : http://study.163.com/u/xiacaojun
https://jiedi.ke.qq.com/
csdn https://edu.csdn.net/course/detail/25037
** 51cto http://edu.51cto.com/lecturer/index/user_id-100013755.html
** http://www.laoxiaketang.com
**
** ---leetcode 296249312
** : jiedi2007
** :
**
*****************************************************************************
// 算法设计与编程实践---基于leetcode的企业真题库 课程 QQ群296249312 下载代码和交流*/
/*
* @lc app=leetcode.cn id=107 lang=cpp
*
* [107] II
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include <vector>
#include <queue>
#include <stack>
using namespace std;
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> v;
stack<vector<int>> s;
queue<TreeNode*> q;
q.push(root);//根节点入队
if(root == NULL)
return v;
while(!q.empty()){
vector<int> vv;
queue<TreeNode*> next;//建立第二个队列
//用来存放下一层的节点
while(!q.empty()){
TreeNode* tre = q.front();
vv.push_back(tre->val);
q.pop();//头元素出队
if(tre->left != NULL)
next.push(tre->left);
if(tre->right != NULL)
next.push(tre->right);
}
s.push(vv);//用一个向量来保存这一层的节点
//进入下一层
q = next;
}
while(!s.empty()){
v.push_back(s.top());
s.pop();
}
return v;
}
};