-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #619 from KBhushan07/main
Leetcode Binary Tree Solutions
- Loading branch information
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
Trees/Leetcode-116. Populating Next Right Pointers in Each Node.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|
||
} | ||
}; |