Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions JunanDang/HW1/21. Merge Two Sorted Lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode dummy(0);
ListNode *prev = &dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
prev->next = l1;
prev = l1;
l1 = l1->next;
} else {
prev->next = l2;
prev = l2;
l2 = l2->next;
}
}
if (l1) {
prev->next = l1;
} else {
prev->next = l2;
}
return dummy.next;
}
};
31 changes: 31 additions & 0 deletions JunanDang/HW1/82. Remove Duplicates from Sorted List II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode dummy(0);
dummy.next = head;

ListNode *curr = head, *pos = &dummy;

while (curr && curr->next) {
if (curr->val == curr->next->val) {
while (curr->next && curr->val == curr->next->val) {
curr = curr->next;
}
curr = curr->next;
pos->next = curr;
}
else {
pos = pos->next;
curr = curr->next;
}
}

return dummy.next;
}
};
28 changes: 28 additions & 0 deletions JunanDang/HW1/92. Reverse Linked List II.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode dummy(0);
dummy.next = head;

ListNode *prev = &dummy;
for (int i = 0; i < m - 1; i++) {
prev = prev->next;
}
ListNode *curr = prev->next;

for (int i = 0; i < n - m; i++) {
ListNode *temp = curr->next;
curr->next = temp->next;
temp->next = prev->next;
prev->next = temp;
}

return dummy.next;
}
};
28 changes: 28 additions & 0 deletions JunanDang/HW2/101. Symmetric Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return root == nullptr || helper(root->left, root->right);
}

bool helper(TreeNode* left, TreeNode* right) {
if (left == nullptr && right == nullptr) {
return true;
}
if (left == nullptr || right == nullptr) {
return false;
}
if (left->val != right->val) {
return false;
}
return helper(left->left, right->right) && helper(left->right, right->left);
}
};
19 changes: 19 additions & 0 deletions JunanDang/HW2/104. Maximum Depth of Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}

return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return helper(inorder, preorder, 0, inorder.size() - 1, 0, preorder.size() - 1);
}

TreeNode* helper(vector<int>& inorder, vector<int>& preorder, int inStart, int inEnd, int preStart, int preEnd) {
if (inStart > inEnd) return nullptr;

int rootVal = preorder[preStart];
TreeNode *root = new TreeNode(rootVal);

int i = inStart;
while (inorder[i] != rootVal) {
i++;
}

root->left = helper(inorder, preorder, inStart, i - 1, preStart + 1, preStart + i - inStart);
root->right = helper(inorder, preorder, i + 1, inEnd, preStart + i - inStart + 1, preEnd);

return root;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
return helper(inorder, postorder, 0, inorder.size() - 1, 0, postorder.size() - 1);
}

TreeNode* helper(vector<int>& inorder, vector<int>& postorder, int inStart, int inEnd, int postStart, int postEnd) {
if (inStart > inEnd) return nullptr;

int rootVal = postorder[postEnd];
TreeNode *root = new TreeNode(rootVal);

int i = inStart;
while (inorder[i] != rootVal) {
i++;
}

root->left = helper(inorder, postorder, inStart, i - 1, postStart, postStart + i - inStart - 1);
root->right = helper(inorder, postorder, i + 1, inEnd, postStart + i - inStart, postEnd - 1);

return root;
}
};
28 changes: 28 additions & 0 deletions JunanDang/HW2/108. Convert Sorted Array to Binary Search Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return helper(nums, 0, nums.size() - 1);
}

TreeNode* helper(vector<int>& nums, int start, int end) {
if (start > end) return nullptr;

int mid = start + (end - start) / 2;

TreeNode *root = new TreeNode(nums[mid]);

root->left = helper(nums, start, mid - 1);
root->right = helper(nums, mid + 1, end);

return root;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode *LCA = root;
while (true) {
if (LCA->val > p->val && LCA->val > q->val) {
LCA = LCA->left;
} else if (LCA->val < p->val && LCA->val < q->val) {
LCA = LCA->right;
} else {
return LCA;
}
}
}
};
27 changes: 27 additions & 0 deletions JunanDang/HW2/236. Lowest Common Ancestor of a Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == nullptr || root == p || root == q) {
return root;
}

TreeNode *left = lowestCommonAncestor(root->left, p, q);
TreeNode *right = lowestCommonAncestor(root->right, p, q);

if (left && right)
return root;
else if (left)
return left;
else
return right;
}
};
47 changes: 47 additions & 0 deletions JunanDang/HW2/257. Binary Tree Paths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
if (root == nullptr) return result;

vector<int> path;
helper(root, path, result);
return result;
}

string genPath(vector<int>& path) {
stringstream ss;
for (int num : path) {
ss << num << "->";
}

string str(ss.str());
str.pop_back();
str.pop_back();
return str;
}

void helper(TreeNode* root, vector<int>& path, vector<string>& result) {
path.push_back(root->val);

if (root->left == nullptr && root->right == nullptr) {
result.push_back(genPath(path));
}

if (root->left)
helper(root->left, path, result);
if (root->right)
helper(root->right, path, result);

path.pop_back();
}
};
15 changes: 15 additions & 0 deletions JunanDang/HW2/96. Unique Binary Search Trees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int numTrees(int n) {
vector<int> f(n + 1, 0);
f[0] = 1;

for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
f[i] += f[j - 1] * f[i - j];
}
}

return f[n];
}
};
32 changes: 32 additions & 0 deletions JunanDang/HW2/98. Validate Binary Search Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
return validBSThelper(root, nullptr, nullptr);
}

bool validBSThelper(TreeNode* root, TreeNode* minnode, TreeNode* maxnode) {
if (root == nullptr) {
return true;
}

bool left = validBSThelper(root->left, minnode, root);
if (!left) return false;
bool right = validBSThelper(root->right, root, maxnode);
if (!right) return false;

if (minnode && minnode->val >= root->val || maxnode && maxnode->val <= root->val) {
return false;
} else {
return true;
}
}
};