Skip to content

Commit

Permalink
folder structure changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul96 committed Nov 4, 2022
1 parent d42db2d commit 385c4a2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Binary Search Tree/Insert into a binary search tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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:
TreeNode *findPosition(TreeNode *root, int val){
if(root->left==NULL && root->right==NULL){
return root;
}
else if(root->val< val && root->right==NULL){
return root;
}
else if(root->val>=val && root->left==NULL){
return root;
}
else if(root->val< val && root->right!=NULL){
return findPosition(root->right,val);
}
else if(root->val>=val && root->left!=NULL){
return findPosition(root->left,val);
}
return NULL;
}
TreeNode* insertIntoBST(TreeNode* root, int val) {
if(root==NULL){
return new TreeNode(val);
}

TreeNode *res = findPosition(root, val);
if(res->val<val){
res->right = new TreeNode(val);
}
else {
res->left = new TreeNode(val);
}
return root;
}
};
20 changes: 20 additions & 0 deletions Trees/Root Equals sum of children.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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:
bool checkTree(TreeNode* root) {
if(root->val == (root->left->val)+ root->right->val ){
return true;
}
return false;
}
};

0 comments on commit 385c4a2

Please sign in to comment.