Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Binary tree Problems #9

Merged
merged 1 commit into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions LeetCodes - Solutions/C++/ConstructBinaryTreePostOrder.cpp
@@ -0,0 +1,50 @@
/**
* 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:

//This function is used to create index mapping for inorder array to get the element present at that position.
void createMapping(vector<int>& inorder, map<int,int>& nodeToIndex, int size) {
for(int i = 0; i < size; i++) {
nodeToIndex[inorder[i]]=i;
}
}

TreeNode* build(vector<int>& inorder, vector<int>& postorder, int &index, int inOrderStart, int inOrderEnd, int n, map<int,int>& nodeToIndex) {
//base case;
if(index < 0 || inOrderStart > inOrderEnd) return NULL;
//take the root node
//as we are travelling from rightmost side of the postOrder array
int element = postorder[index--];
//take the root node
TreeNode* root = new TreeNode(element);
//now find the position of this root node in subtree from inorder tree.
int position = nodeToIndex[element];
//Now recursive calls for left subtree and right subtrees
//Here first of all in case of postorder + inorder --> we have to call right node of the binary tree as in postorder array we are traversing from right side having right node after the root node and then left node
//In case of inorder + preorder --> call left and right node
root->right = build(inorder, postorder, index, position+1, inOrderEnd, n, nodeToIndex);
root->left = build(inorder, postorder, index, inOrderStart, position-1, n, nodeToIndex);
return root;
}

TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
// This is index of initial root
int n = inorder.size();
int postOrderIndex = n - 1;
//create map to store
map<int, int> nodeToIndex;
createMapping(inorder, nodeToIndex, n);
TreeNode* ans = build(inorder, postorder, postOrderIndex, 0, n-1, n, nodeToIndex);
return ans;
}
};
87 changes: 87 additions & 0 deletions LeetCodes - Solutions/C++/SerializeAndDeserializeBinaryTree.cpp
@@ -0,0 +1,87 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:

// Encodes a tree to a single string.
string serialize(TreeNode* root) {
// we are using level order traversal for serializing the binary tree.
// the different is just that we are storing some unique character for NULL to identify the NULL node.
//And also even if left and right nodes are null we are pushing them back to the queue to ensure proper string should get generated along with NULL nodes.
string s="";
if(root == NULL) return s;
queue<TreeNode*> q;
q.push(root);

while(!q.empty()) {
TreeNode* node = q.front();
q.pop();
if(node == NULL) s.append("#,");
else {
s.append(to_string(node->val)+',');
}
if(node != NULL) {
//push left and right nodes
q.push(node->left);
q.push(node->right);
}
}
return s;
}

// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
//For deserialize we are paralley working with queue and string to create binary tree.
//here we are using stringstream to avoid for loops and accordingly by our need fetch the next character in string one by one as it needs
if(data.size() == 0) return NULL;
stringstream s(data);
string str;
getline(s, str, ',');
//First character will always be the root Node
TreeNode* root = new TreeNode(stoi(str));
//Now making queue for generating tree.
queue<TreeNode*> q;
q.push(root);

while(!q.empty()) {
TreeNode* node = q.front();
q.pop();

getline(s, str, ',');

//for left Node
if(str == "#") {
node->left = NULL;
}else {
//store the left node.
// by creating left node
TreeNode* leftNode = new TreeNode(stoi(str));
node->left = leftNode;
q.push(leftNode);
}

//for right Node
getline(s, str, ',');
if(str == "#") {
node->right = NULL;
} else {
TreeNode* rightNode = new TreeNode(stoi(str));
node->right = rightNode;
q.push(rightNode);
}
}
return root;

}
};

// Your Codec object will be instantiated and called as such:
// Codec ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));