-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
ecb0c38
63cc12c
2f4055d
e9f951d
9f23f35
dbeabf0
3aad31e
34cd219
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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). | ||
|
||
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; | ||
} |
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); | ||
} |
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; | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
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; | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
There was a problem hiding this comment.
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.