Skip to content

Commit

Permalink
Merge pull request #619 from KBhushan07/main
Browse files Browse the repository at this point in the history
Leetcode Binary Tree Solutions
  • Loading branch information
gantavyamalviya authored Oct 5, 2022
2 parents ddf0258 + 9c11e2a commit af2b074
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:

Node* connect(Node* root) {
if(root==NULL)
return NULL;

if(root->left){
root->left->next=root->right;

if(root->next){
root->right->next=root->next->left;
}

connect(root->left);
connect(root->right);
}
return root;

}
};
19 changes: 19 additions & 0 deletions Trees/Leetcode-222. Count Complete Tree Nodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int>num;
int countNodes(TreeNode* root) {
if(root==NULL)return 0;
queue<TreeNode*>q;
q.push(root);
while(!q.empty()){
TreeNode*curr = q.front();
num.push_back(curr->val);
if(curr->left!=NULL)
q.push(curr->left);
if(curr->right!=NULL)
q.push(curr->right);
q.pop();
}
return num.size();
}
};
8 changes: 8 additions & 0 deletions Trees/Leetcode-226. Invert Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
TreeNode* invertTree(TreeNode* root) {
if (root) {
invertTree(root->left);
invertTree(root->right);
swap(root->left, root->right);
}
return root;
}
19 changes: 19 additions & 0 deletions Trees/Leetcode-230. Kth Smallest Element in a BST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<int>num;
int kthSmallest(TreeNode* root, int k) {
queue<TreeNode*>q;
q.push(root);
while(!q.empty()){
TreeNode*curr = q.front();
num.push_back(curr->val);
if(curr->left!=NULL)
q.push(curr->left);
if(curr->right!=NULL)
q.push(curr->right);
q.pop();
}
sort(num.begin(), num.end());
return num[k-1];
}
};
82 changes: 82 additions & 0 deletions Trees/Leetcode-623. Add One Row to Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* 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:

// getting all parent nodes for which we want to append nodes
void getBfs(TreeNode* root, int depth , vector<TreeNode*>&v){
if(depth==1 && root!=NULL){
v.push_back(root);
return;
}
if(root==NULL)return;

getBfs(root->left , depth-1 , v);
getBfs(root->right , depth-1 , v);

return;
}

//creating nodes
TreeNode* createNode(int val){
TreeNode *temp=new TreeNode;
temp->val = val;
return temp;
}


TreeNode* addOneRow(TreeNode* root, int val, int depth) {

//if depth ==1
if(depth==1){
TreeNode* temp = createNode(val);
temp ->left = root;
return temp;
}


vector<TreeNode*>v;
getBfs(root , depth-1 , v);
int s = v.size();

for(int i=0; i<s; i++){
TreeNode* temp1 = createNode(val);
TreeNode* temp2 = createNode(val);
TreeNode *t = v[i];
if(t->left && t->right){
temp1->left=t->left;
temp2->right=t->right;
t->left = temp1;
t->right = temp2;
}
else if(t->left && t->right==NULL){
temp1->left=t->left;
t->left = temp1;
t->right = temp2;
}
else if(t->left==NULL && t->right){
temp2->right=t->right;
t->left = temp1;
t->right = temp2;
}
else{
t->left = temp1;
t->right = temp2;
}
}


return root;


}
};

0 comments on commit af2b074

Please sign in to comment.