Skip to content

Commit

Permalink
Create LEVEL_ORDER.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyanshiguptaaa authored Mar 4, 2021
1 parent 5395207 commit f4d4c36
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Trees(Traversal)/LEVEL_ORDER.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
vector<vector<int>> result;

void buildVector(TreeNode *root, int depth)
{
if(root == NULL)
return;
if(result.size() == depth)
result.push_back(vector<int>());

result[depth].push_back(root->val);
buildVector(root->left, depth + 1);
buildVector(root->right, depth + 1);
}

vector<vector<int> > levelOrder(TreeNode *root)
{
buildVector(root, 0);
return result;
}
};

0 comments on commit f4d4c36

Please sign in to comment.