简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career
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
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.
方法很囧,就是把1里面的产生的vector res给reverse一下就行。几乎没什么改变。。不知道有木有不用reverse的办法。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for binary tree | |
* 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) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
vector<int> sol; | |
vector<vector<int> > res; | |
if(!root) return res; | |
queue<TreeNode*> q; | |
int count=1; | |
q.push(root); | |
while(!q.empty()) | |
{ | |
int countnew=0; | |
while(count) | |
{ | |
TreeNode *tmp=q.front(); | |
sol.push_back(tmp->val); | |
q.pop(); | |
count--; | |
if(tmp->left) | |
{ | |
q.push(tmp->left); | |
countnew++; | |
} | |
if(tmp->right) | |
{ | |
q.push(tmp->right); | |
countnew++; | |
} | |
} | |
count=countnew; | |
res.push_back(sol); | |
sol.clear(); | |
} | |
reverse(res.begin(),res.end()); | |
return res; | |
} | |
}; |
No comments:
Post a Comment