很简单的题目,在想是不是后面就不要更这么简答的了,大家都会写,没人看啊。层序遍历的基础上,加了保存每一层,加了从下往上输出,就是一个vector和一个stack的问题嘛,无他,但手熟尔。
class Solution { public: vector<vector<int> > levelOrderBottom(TreeNode *root) { vector<vector<int> > res; if(root == NULL) return res; queue<TreeNode*> que; stack<vector<int> > s; vector<int> tpres; TreeNode *pNode; que.push(root); que.push(NULL); while(!que.empty()){ pNode = que.front(); que.pop(); if(pNode == NULL){ s.push(tpres); tpres.clear(); if(que.empty()) break; else{ que.push(NULL); continue; } } tpres.push_back(pNode->val); if(pNode->left) que.push(pNode->left); if(pNode->right) que.push(pNode->right); } while(!s.empty()){ res.push_back(s.top()); s.pop(); } return res; } };
leetcode第一刷_Binary Tree Level Order Traversal II,布布扣,bubuko.com
leetcode第一刷_Binary Tree Level Order Traversal II
原文地址:http://blog.csdn.net/u012792219/article/details/25278779