/**
* 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
queue<TreeNode*> que;
vector<vector<int> > res;
if (!root)
return res;
que.push(root);
que.push(NULL);
TreeNode* cur;
vector<int> temp;
while (!que.empty())
{
cur = que.front();
if (!cur)
{
if (temp.size() > 0)
{
res.push_back(temp);
temp.clear();
}
if (que.size() > 1)
que.push(NULL);
que.pop();
continue;
}
que.pop();
temp.push_back(cur->val);
if (cur->left)
que.push(cur->left);
if (cur->right)
que.push(cur->right);
}
return res;
}
};
[Leetcode] Binary Tree Level Order Traversal
最新推荐文章于 2024-02-12 01:19:39 发布