/**
* 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<vector<int> > levelorder;
if (!root) {
return levelorder;
}
vector<TreeNode *> seq;
TreeNode *cur;
vector<int> level;
seq.push_back(root);
int begin = 0;
int end;
while (begin < seq.size()) {
end = seq.size();
while (begin < end) {
cur = seq[begin];
if (cur->left) {
seq.push_back(cur->left);
}
if (cur->right) {
seq.push_back(cur->right);
}
level.push_back(cur->val);
begin++;
}
levelorder.push_back(level);
level.clear();
}
return levelorder;
}
};
LeetCode Binary Tree Level Order Traversal
最新推荐文章于 2024-02-12 01:19:39 发布