From dac676dfc644b981596812dcb5aeb7e30580b9f0 Mon Sep 17 00:00:00 2001 From: Shambhavi Gupta <104186704+1103shambhavi@users.noreply.github.com> Date: Sun, 2 Oct 2022 14:03:33 +0530 Subject: [PATCH 1/5] Create Binary Tree Level Order Traversal --- Binary Tree Level Order Traversal | 64 +++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Binary Tree Level Order Traversal diff --git a/Binary Tree Level Order Traversal b/Binary Tree Level Order Traversal new file mode 100644 index 0000000..58f7b4e --- /dev/null +++ b/Binary Tree Level Order Traversal @@ -0,0 +1,64 @@ +/** + * 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) {} + * }; + */ +#include +class Solution +{ + public: + vector> levelOrder(TreeNode* root) + { + vector>ans; + + if(root==NULL) + return ans; + + //creating a queue for level order traversal + queueq; + + //pushing the root node + q.push(root); + + while(!q.empty()) + { + //creating a vector to store the elements at a particular level + vectorlevel; + //extracting the front node from the queue + TreeNode* temp= q.front(); + q.pop(); + + //storing the front node data in level + level.push_back(temp->val); + + //if the front node has left or right or both child push them in the queue + if(temp->left) + q.push(temp->left); + + if(temp->right) + q.push(temp->right); + + //now push all the node data at a particular level in ans vector + ans.push_back(level); + } + + //return ans + return ans; + } +}; + +Test Cases: +Input: root = [3,9,20,null,null,15,7] +Output: [[3],[9,20],[15,7]] + +Input: root = [1] +Output: [[1]] + +Input: root = [] +Output: [] From ca2a1e8c24ebaaa6119ecab14c5d6ef998d7c6e4 Mon Sep 17 00:00:00 2001 From: Shambhavi Gupta <104186704+1103shambhavi@users.noreply.github.com> Date: Sun, 2 Oct 2022 14:14:56 +0530 Subject: [PATCH 2/5] Create Level Order Traversal in Binary Tree This File consists of the code of BFS traversal of a binary tree both the Level order and Reverse Level Order Traversal. --- Level Order Traversal in Binary Tree | 131 +++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 Level Order Traversal in Binary Tree diff --git a/Level Order Traversal in Binary Tree b/Level Order Traversal in Binary Tree new file mode 100644 index 0000000..85c93e5 --- /dev/null +++ b/Level Order Traversal in Binary Tree @@ -0,0 +1,131 @@ +LEVEL ORDER TRAVERSAL +/** + * 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) {} + * }; + */ +#include +class Solution +{ + public: + vector> levelOrder(TreeNode* root) + { + vector>ans; + + if(root==NULL) + return ans; + + //creating a queue for level order traversal + queueq; + + //pushing the root node + q.push(root); + + while(!q.empty()) + { + //creating a vector to store the elements at a particular level + vectorlevel; + //extracting the front node from the queue + TreeNode* temp= q.front(); + q.pop(); + + //storing the front node data in level + level.push_back(temp->val); + + //if the front node has left or right or both child push them in the queue + if(temp->left) + q.push(temp->left); + + if(temp->right) + q.push(temp->right); + + //now push all the node data at a particular level in ans vector + ans.push_back(level); + } + + //return ans + return ans; + } +}; + +Test Cases: +Input: root = [3,9,20,null,null,15,7] +Output: [[3],[9,20],[15,7]] + +Input: root = [1] +Output: [[1]] + +Input: root = [] +Output: [] + + + +//Code for REVERSER LEVEL ORDER TRAVERSAL +/* +logic for this is quiet similar to the level order traversal the only difference is that we have to push right child first then left child and +at the end we have to reverser the level vector before pushing it in ans vector +*/ + + +/** + * 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> levelOrderBottom(TreeNode* root) + { + vector>ans; + + if(root==NULL) + return ans; + + queueq; + q.push(root); + + while(!q.empty()) + { + vectorlevelb; + int size=q.size(); + + for(int i=0;ival); + + if(temp->right) + q.push(temp->right); + + if(temp->left) + q.push(temp->left); + } + reverse(levelb.begin(),levelb.end()); + ans.push_back(levelb); + } + reverse(ans.begin(),ans.end()); + + + return ans; + } +}; + +Test Cases: +Input: root = [3,9,20,null,null,15,7] +Output: [[15,7],[9,20],[3]] + +Input: root = [1] +Output: [[1]] From e0517ac92d19b9a7c5463cadc442e43e5d8ef57b Mon Sep 17 00:00:00 2001 From: Shambhavi Gupta <104186704+1103shambhavi@users.noreply.github.com> Date: Sun, 2 Oct 2022 14:15:33 +0530 Subject: [PATCH 3/5] Delete Binary Tree Level Order Traversal --- Binary Tree Level Order Traversal | 64 ------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 Binary Tree Level Order Traversal diff --git a/Binary Tree Level Order Traversal b/Binary Tree Level Order Traversal deleted file mode 100644 index 58f7b4e..0000000 --- a/Binary Tree Level Order Traversal +++ /dev/null @@ -1,64 +0,0 @@ -/** - * 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) {} - * }; - */ -#include -class Solution -{ - public: - vector> levelOrder(TreeNode* root) - { - vector>ans; - - if(root==NULL) - return ans; - - //creating a queue for level order traversal - queueq; - - //pushing the root node - q.push(root); - - while(!q.empty()) - { - //creating a vector to store the elements at a particular level - vectorlevel; - //extracting the front node from the queue - TreeNode* temp= q.front(); - q.pop(); - - //storing the front node data in level - level.push_back(temp->val); - - //if the front node has left or right or both child push them in the queue - if(temp->left) - q.push(temp->left); - - if(temp->right) - q.push(temp->right); - - //now push all the node data at a particular level in ans vector - ans.push_back(level); - } - - //return ans - return ans; - } -}; - -Test Cases: -Input: root = [3,9,20,null,null,15,7] -Output: [[3],[9,20],[15,7]] - -Input: root = [1] -Output: [[1]] - -Input: root = [] -Output: [] From 3178987e2a387179df2fef5d23f1c50cb4c5d00a Mon Sep 17 00:00:00 2001 From: Shambhavi Gupta <104186704+1103shambhavi@users.noreply.github.com> Date: Sun, 2 Oct 2022 14:19:59 +0530 Subject: [PATCH 4/5] Delete Level Order Traversal in Binary Tree --- Level Order Traversal in Binary Tree | 131 --------------------------- 1 file changed, 131 deletions(-) delete mode 100644 Level Order Traversal in Binary Tree diff --git a/Level Order Traversal in Binary Tree b/Level Order Traversal in Binary Tree deleted file mode 100644 index 85c93e5..0000000 --- a/Level Order Traversal in Binary Tree +++ /dev/null @@ -1,131 +0,0 @@ -LEVEL ORDER TRAVERSAL -/** - * 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) {} - * }; - */ -#include -class Solution -{ - public: - vector> levelOrder(TreeNode* root) - { - vector>ans; - - if(root==NULL) - return ans; - - //creating a queue for level order traversal - queueq; - - //pushing the root node - q.push(root); - - while(!q.empty()) - { - //creating a vector to store the elements at a particular level - vectorlevel; - //extracting the front node from the queue - TreeNode* temp= q.front(); - q.pop(); - - //storing the front node data in level - level.push_back(temp->val); - - //if the front node has left or right or both child push them in the queue - if(temp->left) - q.push(temp->left); - - if(temp->right) - q.push(temp->right); - - //now push all the node data at a particular level in ans vector - ans.push_back(level); - } - - //return ans - return ans; - } -}; - -Test Cases: -Input: root = [3,9,20,null,null,15,7] -Output: [[3],[9,20],[15,7]] - -Input: root = [1] -Output: [[1]] - -Input: root = [] -Output: [] - - - -//Code for REVERSER LEVEL ORDER TRAVERSAL -/* -logic for this is quiet similar to the level order traversal the only difference is that we have to push right child first then left child and -at the end we have to reverser the level vector before pushing it in ans vector -*/ - - -/** - * 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> levelOrderBottom(TreeNode* root) - { - vector>ans; - - if(root==NULL) - return ans; - - queueq; - q.push(root); - - while(!q.empty()) - { - vectorlevelb; - int size=q.size(); - - for(int i=0;ival); - - if(temp->right) - q.push(temp->right); - - if(temp->left) - q.push(temp->left); - } - reverse(levelb.begin(),levelb.end()); - ans.push_back(levelb); - } - reverse(ans.begin(),ans.end()); - - - return ans; - } -}; - -Test Cases: -Input: root = [3,9,20,null,null,15,7] -Output: [[15,7],[9,20],[3]] - -Input: root = [1] -Output: [[1]] From 1d315957724f54cd8108acde4676f7af0833435f Mon Sep 17 00:00:00 2001 From: Shambhavi Gupta <104186704+1103shambhavi@users.noreply.github.com> Date: Sun, 2 Oct 2022 14:22:05 +0530 Subject: [PATCH 5/5] Create Level Order Traversal in Binary Tree This file consist of the solution for 2 Leetcode problems Binary Tree Level Order Traversal and Binary Tree Level Order Traversal II --- Level Order Traversal in Binary Tree | 131 +++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 Level Order Traversal in Binary Tree diff --git a/Level Order Traversal in Binary Tree b/Level Order Traversal in Binary Tree new file mode 100644 index 0000000..85c93e5 --- /dev/null +++ b/Level Order Traversal in Binary Tree @@ -0,0 +1,131 @@ +LEVEL ORDER TRAVERSAL +/** + * 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) {} + * }; + */ +#include +class Solution +{ + public: + vector> levelOrder(TreeNode* root) + { + vector>ans; + + if(root==NULL) + return ans; + + //creating a queue for level order traversal + queueq; + + //pushing the root node + q.push(root); + + while(!q.empty()) + { + //creating a vector to store the elements at a particular level + vectorlevel; + //extracting the front node from the queue + TreeNode* temp= q.front(); + q.pop(); + + //storing the front node data in level + level.push_back(temp->val); + + //if the front node has left or right or both child push them in the queue + if(temp->left) + q.push(temp->left); + + if(temp->right) + q.push(temp->right); + + //now push all the node data at a particular level in ans vector + ans.push_back(level); + } + + //return ans + return ans; + } +}; + +Test Cases: +Input: root = [3,9,20,null,null,15,7] +Output: [[3],[9,20],[15,7]] + +Input: root = [1] +Output: [[1]] + +Input: root = [] +Output: [] + + + +//Code for REVERSER LEVEL ORDER TRAVERSAL +/* +logic for this is quiet similar to the level order traversal the only difference is that we have to push right child first then left child and +at the end we have to reverser the level vector before pushing it in ans vector +*/ + + +/** + * 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> levelOrderBottom(TreeNode* root) + { + vector>ans; + + if(root==NULL) + return ans; + + queueq; + q.push(root); + + while(!q.empty()) + { + vectorlevelb; + int size=q.size(); + + for(int i=0;ival); + + if(temp->right) + q.push(temp->right); + + if(temp->left) + q.push(temp->left); + } + reverse(levelb.begin(),levelb.end()); + ans.push_back(levelb); + } + reverse(ans.begin(),ans.end()); + + + return ans; + } +}; + +Test Cases: +Input: root = [3,9,20,null,null,15,7] +Output: [[15,7],[9,20],[3]] + +Input: root = [1] +Output: [[1]]