层次遍历
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ret;
if(root==NULL) return ret;
vector<int> arr;
TreeNode* p;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
arr.clear();
root=q.back();
do{
p=q.front();
q.pop();
arr.push_back(p->val);
if(p->left) q.push(p->left);
if(p->right) q.push(p->right);
}while(p!=root);
ret.push_back(arr);
}
return ret;
}
};