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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission Week Six #6

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions week06/LCA_of_a_binary_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Time complexity: O(N) , N = Number of nodes.
// Space complexity: O(1).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space complexity is O(H), where H = max depth of the tree. It is because of the stack memory used by the recursion.


TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q){
if(!root) return NULL;
if(root->val == p->val || root->val == q->val) return root;
TreeNode* leftNode = lowestCommonAncestor(root->left, p, q);
TreeNode* rightNode = lowestCommonAncestor(root->right, p, q);
if(leftNode && rightNode) return root;
return leftNode ? leftNode : rightNode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time complexity: O(N), N = Number of Nodes.
// Space complexity: O(N), N = Number of Nodes.

TreeNode* treeBuilding(vector<int> &preorder, vector<int> &inorder, int left, int right, int &pos, unordered_map<int, int> &pathMap){
if(left > right) return NULL;
TreeNode* currNode = new TreeNode(preorder[pos++]);
int mid = pathMap[currNode->val];
currNode->left = treeBuilding(preorder, inorder, left, mid - 1, pos, pathMap);
currNode->right = treeBuilding(preorder, inorder, mid + 1, right, pos, pathMap);
return currNode;
}

TreeNode* buildTree(vector<int> &preorder, vector<int> &inorder){
int pos = 0;
unordered_map<int, int> pathMap;
int arrLength = inorder.size();
for(int i = 0; i < arrLength; i++){
pathMap[inorder[i]] = i;
}
TreeNode* root = treeBuilding(preorder, inorder, 0, preorder.size() - 1, pos, pathMap);
return root;
}
17 changes: 17 additions & 0 deletions week06/convert_sorted_array_to_binary_search_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Time complexity: O(N), N = Number of nodes;
// Space complexity: O(N), N = Number of nodes;

TreeNode* buildBST(vector<int>& nums, int left, int right){
if(left > right) return NULL;

int mid = left + (right - left) / 2;
TreeNode* currNode = new TreeNode(nums[mid]);
currNode->left = buildBST(nums, left, mid - 1);
currNode->right = buildBST(nums, mid + 1, right);

return currNode;
}

TreeNode* sortedArray(vector<int>& nums){
return buildBST(nums, 0, nums.size() - 1);
}
19 changes: 19 additions & 0 deletions week06/kth_smallest_element_in_a_bst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Time complexity: O(N), N = Number of nodes;
// Space complexity: O(1)


void traverseTree(TreeNode* root, int &k, int &kthValue){
if(!root) return;
traverseTree(root->left, k, kthValue);
k--;
if(k == 0){
kthValue = root->val;
}
traverseTree(root->right, k, kthValue);
}

int kthSmallest(TreeNode* root, int k){
int kthValue;
traverseTree(root, k, kthValue);
return kthValue;
}
7 changes: 7 additions & 0 deletions week06/maximum_depth_of_binary_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Time complexity: O(N), N = Number of nodes
// Space complexity: O(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space complexity is O(H), where H = max depth of the tree. It is because of the stack memory used by the recursion.


int maxDepth(TreeNode* root, int depth = 0){
if(!root) return depth;
return max(maxDepth(root->left, depth + 1), maxDepth(root->right, depth + 1));
}
16 changes: 16 additions & 0 deletions week06/populating_next_right_pointers_in_each_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Time complexity: O(N), N = Number of nodes
// Space complexity: O(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space complexity is O(H), where H = max depth of the tree. It is because of the stack memory used by the recursion.


void traverseNode(Node* node){
if(!root) return;
if(node->left) node->left->next = node->right;
if(node->right && node->next) node->right->next = node->next->left;

if(node->left) traverseNode(node->left);
if(node->right) traverseNode(node->right);
}

Node* connect(Node* root){
traverseNode(root);
return root;
}
31 changes: 31 additions & 0 deletions week06/symmetric_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Time complexity: O(N), N = Number of nodes;
// Space complexity: O(N), N = Number of nodes;

void nodeTraverseLeft(TreeNode* node, vector<int> &u){
if(!node){
u.push_back(1000); // i chose a random value outside of range.
return;
}
u.push_back(node->val);
nodeTraversalLeft(node->left, u);
nodeTraversalLeft(node->right, u);
}
void nodeTraverseRight(TreeNode* node, vector<int> &v){
if(!node){
v.push_back(1000); // i chose a random value outside of range.
return;
}
v.push_back(node->val);
nodeTraversalLeft(node->left, v);
nodeTraversalLeft(node->right, v);
}

bool isSymmetric(TreeNode* root){
vector<int> a, b;
nodeTraverseLeft(root->left, a);
nodeTraverseRight(root->right, a);
for(int i = 0; i < a.size(); i++){
if(a[i] != b[i]) return false;
}
return true;
}
15 changes: 15 additions & 0 deletions week06/validate_binary_search_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Time complexity: O(N), N = Number of nodes;
// Space complexity: O(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space complexity is O(H), where H = max depth of the tree. It is because of the stack memory used by the recursion.


bool isBST(TreeNode* node, long long left, long long right){
if(!root) return true;
long long node_value = node->val;
if(node_value >= left && node_value <= right){
return isBST(node->left, left, node_value - 1) && isBST(node->right, node_value + 1, right);
}
return false;
}

bool isValidBST(TreeNode* root){
return isBST(root, LLONG_MIN, LLONG_MAX);
}