Skip to content
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
hashset = set()

for n in nums:
if n in hashset:
return True
hashset.add(n)
return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
maxSub = nums[0]
curSum = 0

for n in nums:
if curSum < 0:
curSum = 0
curSum += n
maxSub = max(maxSub, curSum)
return maxSub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Solution {
List<Integer> result = new ArrayList<Integer>();

public List<Integer> inorderTraversal(TreeNode root) {
if(root !=null){
helper(root);
}

return result;
}

public void helper(TreeNode p){
if(p.left!=null)
helper(p.left);

result.add(p.val);

if(p.right!=null)
helper(p.right);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root==null) {
return res;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode temp = stack.peek();
if(temp.left==null && temp.right==null) {
TreeNode pop = stack.pop();
res.add(pop.val);
}
else {
if(temp.right!=null) {
stack.push(temp.right);
temp.right = null;
}
if(temp.left!=null) {
stack.push(temp.left);
temp.left = null;
}
}
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> v;
preTraversal(root, v);
return v;
}
void preTraversal(TreeNode* root, vector<int>& v){
if(!root) return;
v.push_back(root->val);
preTraversal(root->left, v);
preTraversal(root->right, v);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
queue, ans = deque([root] if root else []), []
while len(queue):
qlen, row = len(queue), []
for _ in range(qlen):
curr = queue.popleft()
row.append(curr.val)
if curr.left: queue.append(curr.left)
if curr.right: queue.append(curr.right)
ans.append(row)
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public int maxDepth(TreeNode root) {
if(root==null)
return 0;

int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);

int bigger = Math.max(leftDepth, rightDepth);

return bigger+1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
bool check(TreeNode* root1,TreeNode* root2){
if(root1==nullptr or root2==nullptr){
return root1==root2;
}
return root1->val==root2->val and check(root1->left,root2->right) and check(root1->right,root2->left);
}
bool isSymmetric(TreeNode* root) {
return check(root,root);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
void swap(TreeNode *curr)
{
if(!curr)
return;
swap(curr->left);
swap(curr->right);
TreeNode *temp;
temp = curr->left;
curr->left = curr->right;
curr->right = temp;
}
public:
TreeNode* invertTree(TreeNode* root) {
swap(root);
return root;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null)
return false;
if (root.val == sum && (root.left == null && root.right == null))
return true;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
TreeNode node = root;
while (node != null) {
if (val > node.val) {
if (node.right == null) {
node.right = new TreeNode(val);
return root;
} else {
node = node.right;
}
} else {
if (node.left == null) {
node.left = new TreeNode(val);
return root;
} else {
node = node.left;
}
}
}
return new TreeNode(val);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
while(root && root->val!=val){
if(root->val < val){
root=root->right;
}else{
root=root->left;
}
}
return root;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if(root==NULL)
return NULL;
else if (root->val == val)
return root;
else if (root->val < val)
return searchBST(root->right,val);
else
return searchBST(root->left,val);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode m = root;
if(m.val > p.val && m.val < q.val){
return m;
}else if(m.val>p.val && m.val > q.val){
return lowestCommonAncestor(root.left, p, q);
}else if(m.val<p.val && m.val < q.val){
return lowestCommonAncestor(root.right, p, q);
}
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public boolean findTarget(TreeNode root, int k) {
List<Integer> nums = new ArrayList<>();
inorder(root, nums);
for(int i = 0, j = nums.size()-1; i<j;){
if(nums.get(i) + nums.get(j) == k)return true;
if(nums.get(i) + nums.get(j) < k)i++;
else j--;
}
return false;
}
public void inorder(TreeNode root, List<Integer> nums){
if(root == null)return;
inorder(root.left, nums);
nums.add(root.val);
inorder(root.right, nums);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public boolean isValidBST(TreeNode root) {
return isValidBST(root,
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY);
}
public boolean isValidBST(TreeNode p, double min, double max){
if(p==null)
return true;
if(p.val <= min || p.val >= max)
return false;
return isValidBST(p.left, min, p.val) && isValidBST(p.right, p.val, max);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
// Two Pointer Approach
int p1 = m-1;
int p2 = n-1;
int i = m+n-1;
while(p2 >= 0){
if(p1 >= 0 && nums1[p1] > nums2[p2]){
nums1[i] = nums1[p1];
i--; p1--;
} else {
nums1[i] = nums2[p2];
i--; p2--;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {

int j=0;
for(int i=m;i<nums1.length;i++){
nums1[i] = nums2[j];
j++;
}
Arrays.sort(nums1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (nums[i] + nums[j] == target)
{
return{i, j};
}
}
}

return{-1, -1};
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int maxProfit(int[] prices) {
if(prices==null||prices.length<=1)
return 0;
int min=prices[0];
int result=0;
for(int i=1; i<prices.length; i++){
result = Math.max(result, prices[i]-min);
min = Math.min(min, prices[i]);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) {
return new int[0];
}
int i = 0;
int j = 0;

Arrays.sort(nums1);
Arrays.sort(nums2);

List<Integer> result = new ArrayList<>();

while (i < nums1.length && j < nums2.length) {
if (nums1[i] == nums2[j]) {
result.add(nums1[i]);
i++;
j++;
}
else if (nums1[i] < nums2[j]){
i++;
}
else {
j++;
}
}
return listToArray(result);
}

private int[] listToArray(List<Integer> list) {
int[] result = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> r(numRows);
for (int i = 0; i < numRows; i++) {
r[i].resize(i + 1);
r[i][0] = r[i][i] = 1;

for (int j = 1; j < i; j++)
r[i][j] = r[i - 1][j - 1] + r[i - 1][j];
}
return r;
}
};
Loading