Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- #include <queue>
- class Solution {
- public:
- vector<vector<int>> levelOrder(TreeNode* root) {
- vector<vector<int>> result;
- if(!root)
- return result;
- TreeNode* node = nullptr;
- queue<TreeNode*> q;
- q.push(root);
- q.push(nullptr);
- vector<int> v;
- while(!q.empty())
- {
- node = q.front();
- q.pop();
- if(q.empty()) {
- result.push_back(v);
- break;
- }
- if(node) {
- if(node->left)
- q.push(node->left);
- if(node->right)
- q.push(node->right);
- v.push_back(node->val);
- } else {
- q.push(nullptr);
- result.push_back(v);
- v.clear();
- }
- }
- return result;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement