简历: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 level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree
Given binary tree
{3,9,20,#,#,15,7}
,3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
confused what
» Solve this problem"{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.这题就是BFS,不论是图还是树的一种基本遍历的方法,应该掌握,比较简单,我用了2个queue来存,其实不需要,用一个count来计数每一层的node的个数,我使用了一个queue在level order traversal ii里面。
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> > levelOrder(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 *> q1, q2; | |
sol.push_back(root->val); | |
res.push_back(sol); | |
sol.clear(); | |
if(root->left) q1.push(root->left); | |
if(root->right) q1.push(root->right); | |
while(!q1.empty()||!q2.empty()) | |
{ | |
if(!q1.empty()) | |
{ | |
while(!q1.empty()) | |
{ | |
TreeNode *tmp=q1.front(); | |
q1.pop(); | |
sol.push_back(tmp->val); | |
if(tmp->left) q2.push(tmp->left); | |
if(tmp->right) q2.push(tmp->right); | |
} | |
res.push_back(sol); | |
sol.clear(); | |
} | |
if(!q2.empty()) | |
{ | |
while(!q2.empty()) | |
{ | |
TreeNode *tmp=q2.front(); | |
q2.pop(); | |
sol.push_back(tmp->val); | |
if(tmp->left) q1.push(tmp->left); | |
if(tmp->right) q1.push(tmp->right); | |
} | |
res.push_back(sol); | |
sol.clear(); | |
} | |
} | |
return res; | |
} | |
}; |
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> > levelOrder(TreeNode *root) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
vector<int> tmp; | |
vector<vector<int>> res; | |
if(!root) return res; | |
queue<TreeNode*> level; | |
level.push(root); | |
int count=0; | |
while(!level.empty()) | |
{ | |
count=level.size(); | |
while(count) | |
{ | |
TreeNode* cur=level.front(); | |
tmp.push_back(cur->val); | |
count--; | |
level.pop(); | |
if(cur->left) | |
level.push(cur->left); | |
if(cur->right) | |
level.push(cur->right); | |
} | |
res.push_back(tmp); | |
tmp.clear(); | |
} | |
return res; | |
} | |
}; |
I always thought of using two queues or vectors. Never thought of using only one queue with simply counters. Grateful for being inspired by your blog. Thanks.
ReplyDelete