0% found this document useful (0 votes)
35 views

Important Level Order Traversal - Leetcode 102

The document discusses three different solutions for level order tree traversal in Leetcode: 1. A general recursive method that stores the traversal in a 2D vector result. It recursively calls a print function that populates the result vector. 2. An iterative method using a queue. It pushes the root to the queue and in a loop dequeues nodes, processes their children and value, and pushes children to the queue. It stores the level values in a temporary vector that gets added to the result after each level. 3. A solution for zigzag level order traversal. It uses the same recursive print function but reverses the order of even-indexed levels in the result vector before returning.

Uploaded by

YASH PALIWAL
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Important Level Order Traversal - Leetcode 102

The document discusses three different solutions for level order tree traversal in Leetcode: 1. A general recursive method that stores the traversal in a 2D vector result. It recursively calls a print function that populates the result vector. 2. An iterative method using a queue. It pushes the root to the queue and in a loop dequeues nodes, processes their children and value, and pushes children to the queue. It stores the level values in a temporary vector that gets added to the result after each level. 3. A solution for zigzag level order traversal. It uses the same recursive print function but reverses the order of even-indexed levels in the result vector before returning.

Uploaded by

YASH PALIWAL
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

********************** leetcode level order solution *********************

********* 1. general recursion methos=d **************************


/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class Solution {
public:vector<vector<int>> res;
vector<vector<int>> levelOrder(TreeNode* root) {
print(root,0);
return res;
}
void print(TreeNode* root,int level)
{
if(root==NULL)
{
return ;
}
if(res.size()==level)
{
vector<int> vala; // stroing new null vector if we ran out of order
res.push_back(vala);
}
res[level].push_back(root->val);
print(root->left,level+1);
print(root->right,level+1);

}
};

********************* 2. using queue ****************************


/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root==NULL)
{
return res;
}
queue<TreeNode*>q;
q.push(root);
while(!q.empty())
{
int n=q.size();
vector<int> temp;
while(n)
{
TreeNode* node=q.front();
temp.push_back(node->val);
q.pop();
if(node->left)
{
q.push(node->left);
}
if(node->right)
{
q.push(node->right);
}
n--;
}
res.push_back(temp);
}return res;
}

};

********************** leetcode level order zigzag solution


103*********************

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
right(right) {}
* };
*/
class Solution {
public:vector<vector<int>> res;
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
print(root,0);
int n = res.size();
for(int i=0;i<n;i++)
{if(i%2!=0)
{
reverse(res[i].begin(),res[i].end());
}
}
return res;
}
void print(TreeNode* root,int level)
{
if(root==NULL)
{
return ;
}
if(res.size()==level)
{vector<int> vala;
res.push_back(vala);
}
res[level].push_back(root->val);
print(root->left,level+1);
print(root->right,level+1);
}
};

You might also like