diff --git a/JunanDang/HW1/21. Merge Two Sorted Lists.cpp b/JunanDang/HW1/21. Merge Two Sorted Lists.cpp new file mode 100644 index 0000000..39ae4e4 --- /dev/null +++ b/JunanDang/HW1/21. Merge Two Sorted Lists.cpp @@ -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; + } +}; diff --git a/JunanDang/HW1/82. Remove Duplicates from Sorted List II.cpp b/JunanDang/HW1/82. Remove Duplicates from Sorted List II.cpp new file mode 100644 index 0000000..f744e2a --- /dev/null +++ b/JunanDang/HW1/82. Remove Duplicates from Sorted List II.cpp @@ -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; + } +}; diff --git a/JunanDang/HW1/92. Reverse Linked List II.cpp b/JunanDang/HW1/92. Reverse Linked List II.cpp new file mode 100644 index 0000000..2a3e1e1 --- /dev/null +++ b/JunanDang/HW1/92. Reverse Linked List II.cpp @@ -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; + } +}; diff --git a/JunanDang/HW2/101. Symmetric Tree.cpp b/JunanDang/HW2/101. Symmetric Tree.cpp new file mode 100644 index 0000000..2b076f5 --- /dev/null +++ b/JunanDang/HW2/101. Symmetric Tree.cpp @@ -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); + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/104. Maximum Depth of Binary Tree.cpp b/JunanDang/HW2/104. Maximum Depth of Binary Tree.cpp new file mode 100644 index 0000000..7835431 --- /dev/null +++ b/JunanDang/HW2/104. Maximum Depth of Binary Tree.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/105. Construct Binary Tree from Preorder and Inorder Traversal.cpp b/JunanDang/HW2/105. Construct Binary Tree from Preorder and Inorder Traversal.cpp new file mode 100644 index 0000000..1411a37 --- /dev/null +++ b/JunanDang/HW2/105. Construct Binary Tree from Preorder and Inorder Traversal.cpp @@ -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& preorder, vector& inorder) { + return helper(inorder, preorder, 0, inorder.size() - 1, 0, preorder.size() - 1); + } + + TreeNode* helper(vector& inorder, vector& 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; + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/106. Construct Binary Tree from Inorder and Postorder Traversal.cpp b/JunanDang/HW2/106. Construct Binary Tree from Inorder and Postorder Traversal.cpp new file mode 100644 index 0000000..cc704b8 --- /dev/null +++ b/JunanDang/HW2/106. Construct Binary Tree from Inorder and Postorder Traversal.cpp @@ -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& inorder, vector& postorder) { + return helper(inorder, postorder, 0, inorder.size() - 1, 0, postorder.size() - 1); + } + + TreeNode* helper(vector& inorder, vector& 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; + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/108. Convert Sorted Array to Binary Search Tree.cpp b/JunanDang/HW2/108. Convert Sorted Array to Binary Search Tree.cpp new file mode 100644 index 0000000..132704b --- /dev/null +++ b/JunanDang/HW2/108. Convert Sorted Array to Binary Search Tree.cpp @@ -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& nums) { + return helper(nums, 0, nums.size() - 1); + } + + TreeNode* helper(vector& 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; + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/235. Lowest Common Ancestor of a Binary Search Tree.cpp b/JunanDang/HW2/235. Lowest Common Ancestor of a Binary Search Tree.cpp new file mode 100644 index 0000000..340b863 --- /dev/null +++ b/JunanDang/HW2/235. Lowest Common Ancestor of a Binary Search Tree.cpp @@ -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; + } + } + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/236. Lowest Common Ancestor of a Binary Tree.cpp b/JunanDang/HW2/236. Lowest Common Ancestor of a Binary Tree.cpp new file mode 100644 index 0000000..33d0644 --- /dev/null +++ b/JunanDang/HW2/236. Lowest Common Ancestor of a Binary Tree.cpp @@ -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; + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/257. Binary Tree Paths.cpp b/JunanDang/HW2/257. Binary Tree Paths.cpp new file mode 100644 index 0000000..8298ecc --- /dev/null +++ b/JunanDang/HW2/257. Binary Tree Paths.cpp @@ -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 binaryTreePaths(TreeNode* root) { + vector result; + if (root == nullptr) return result; + + vector path; + helper(root, path, result); + return result; + } + + string genPath(vector& 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& path, vector& 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(); + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/96. Unique Binary Search Trees.cpp b/JunanDang/HW2/96. Unique Binary Search Trees.cpp new file mode 100644 index 0000000..7591a41 --- /dev/null +++ b/JunanDang/HW2/96. Unique Binary Search Trees.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int numTrees(int n) { + vector 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]; + } +}; \ No newline at end of file diff --git a/JunanDang/HW2/98. Validate Binary Search Tree.cpp b/JunanDang/HW2/98. Validate Binary Search Tree.cpp new file mode 100644 index 0000000..09af41b --- /dev/null +++ b/JunanDang/HW2/98. Validate Binary Search Tree.cpp @@ -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; + } + } +}; \ No newline at end of file