-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4bf288
commit dcabb80
Showing
18 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
leetcode/problems/cpp/largest-local-values-in-a-matrix.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "vector" | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> largestLocal(vector<vector<int>>& grid) { | ||
int n = grid.size(); | ||
|
||
for (int i = 1; i < n - 1; ++i) { | ||
for (int j = 1; j < n - 1; ++j) { | ||
int temp = 0; | ||
|
||
for (int k = i - 1; k <= i + 1; ++k) { | ||
for (int l = j - 1; l <= j + 1; ++l) { | ||
temp = max(temp, grid[k][l]); | ||
} | ||
} | ||
|
||
grid[i - 1][j - 1] = temp; | ||
} | ||
} | ||
|
||
// resizing | ||
grid.resize(n - 2); | ||
for (int i = 0; i < grid.size(); ++i) { | ||
grid[i].resize(n - 2); | ||
} | ||
|
||
return grid; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public boolean isBalanced(TreeNode root) { | ||
return dfs(root) != -1; | ||
} | ||
|
||
int dfs(TreeNode root) { | ||
if (root == null) | ||
return 0; | ||
int left = dfs(root.left); | ||
if (left == -1) | ||
return -1; | ||
int right = dfs(root.right); | ||
if (right == -1) | ||
return -1; | ||
if (Math.abs(left - right) > 1) | ||
return -1; | ||
return 1 + Math.max(left, right); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
leetcode/problems/java/binary-tree-level-order-traversal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
class Solution { | ||
public List<List<Integer>> levelOrder(TreeNode root) { | ||
List<List<Integer>> levels = new ArrayList<>(); | ||
if (root == null) | ||
return levels; | ||
List<TreeNode> level = new ArrayList<>(); | ||
level.add(root); | ||
while (!level.isEmpty()) { | ||
List<TreeNode> nextLevel = new ArrayList<>(); | ||
List<Integer> values = new ArrayList<>(); | ||
for (TreeNode node : level) { | ||
values.add(node.val); | ||
if (node.left != null) | ||
nextLevel.add(node.left); | ||
if (node.right != null) | ||
nextLevel.add(node.right); | ||
} | ||
levels.add(values); | ||
level = nextLevel; | ||
} | ||
return levels; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public int maxPathSum(TreeNode root) { | ||
var result = new int[] { Integer.MIN_VALUE }; | ||
maxPathSum(root, result); | ||
return result[0]; | ||
} | ||
|
||
int maxPathSum(TreeNode root, int[] result) { | ||
if (root == null) { | ||
return 0; | ||
} | ||
int left = Math.max(maxPathSum(root.left, result), 0); | ||
int right = Math.max(maxPathSum(root.right, result), 0); | ||
result[0] = Math.max(left + right + root.val, result[0]); | ||
return Math.max(left, right) + root.val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
class Solution { | ||
public List<Integer> rightSideView(TreeNode root) { | ||
List<Integer> rightSide = new ArrayList<>(); | ||
rightView(root, 0, rightSide); | ||
return rightSide; | ||
} | ||
|
||
void rightView(TreeNode root, int level, List<Integer> rightSide) { | ||
if (root == null) | ||
return; | ||
if (level == rightSide.size()) | ||
rightSide.add(root.val); | ||
rightView(root.right, level + 1, rightSide); | ||
rightView(root.left, level + 1, rightSide); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
leetcode/problems/java/construct-binary-tree-from-preorder-and-inorder-traversal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import java.util.HashMap; | ||
|
||
class Solution { | ||
int pi = 0; | ||
|
||
public TreeNode buildTree(int[] preorder, int[] inorder) { | ||
HashMap<Integer, Integer> inorderMap = new HashMap<>(); | ||
for (int i = 0; i < inorder.length; i++) | ||
inorderMap.put(inorder[i], i); | ||
return solve(preorder, inorderMap, 0, inorder.length); | ||
} | ||
|
||
TreeNode solve(int[] preorder, HashMap<Integer, Integer> inorderMap, int left, int right) { | ||
if (left == right) | ||
return null; | ||
TreeNode root = new TreeNode(preorder[pi++]); | ||
|
||
int inorderIndex = inorderMap.get(root.val); | ||
root.left = solve(preorder, inorderMap, left, inorderIndex); | ||
root.right = solve(preorder, inorderMap, inorderIndex + 1, right); | ||
|
||
return root; | ||
|
||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
leetcode/problems/java/count-good-nodes-in-binary-tree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
int count = 0; | ||
|
||
public int goodNodes(TreeNode root) { | ||
dfs(root, root.val); | ||
return count; | ||
} | ||
|
||
void dfs(TreeNode root, int max) { | ||
if (root == null) | ||
return; | ||
if (root.val >= max) { | ||
count++; | ||
max = root.val; | ||
} | ||
dfs(root.left, max); | ||
dfs(root.right, max); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
int ans = 0; | ||
|
||
public int diameterOfBinaryTree(TreeNode root) { | ||
dfs(root); | ||
return ans; | ||
} | ||
|
||
int dfs(TreeNode root) { | ||
if (root == null) | ||
return 0; | ||
int left = dfs(root.left); | ||
int right = dfs(root.right); | ||
ans = Math.max(ans, left + right); | ||
return 1 + Math.max(left, right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution { | ||
public boolean evaluateTree(TreeNode root) { | ||
if (root == null) | ||
return false; | ||
boolean left = evaluateTree(root.left); | ||
if (left) | ||
if (root.val == 2) | ||
return true; | ||
if (!left) | ||
if (root.val == 3) | ||
return false; | ||
if (root.val == 2) | ||
return left || evaluateTree(root.right); | ||
if (root.val == 3) | ||
return left && evaluateTree(root.right); | ||
if (root.val == 0) | ||
return false; | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
class Solution { | ||
public TreeNode invertTree(TreeNode root) { | ||
if (root == null) | ||
return null; | ||
TreeNode left = invertTree(root.left); | ||
TreeNode right = invertTree(root.right); | ||
root.left = right; | ||
root.right = left; | ||
return root; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution { | ||
int n, ans = 0; | ||
|
||
public int kthSmallest(TreeNode root, int k) { | ||
n = k; | ||
dfs(root); | ||
return ans; | ||
} | ||
|
||
void dfs(TreeNode root) { | ||
if (root == null || n == 0) | ||
return; | ||
dfs(root.left); | ||
if (n == 1) { | ||
ans = root.val; | ||
n--; | ||
return; | ||
} else { | ||
n--; | ||
} | ||
dfs(root.right); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
leetcode/problems/java/lowest-common-ancestor-of-a-binary-search-tree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
TreeNode curr = root; | ||
while (curr != null) { | ||
if (p.val < curr.val && q.val < curr.val) { | ||
curr = curr.left; | ||
} else if (p.val > curr.val && q.val > curr.val) { | ||
curr = curr.right; | ||
} else { | ||
return curr; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
leetcode/problems/java/maximize-happiness-of-selected-children.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import java.util.Arrays; | ||
|
||
class Solution { | ||
public long maximumHappinessSum(int[] happiness, int k) { | ||
Arrays.sort(happiness); | ||
long res = 0; | ||
int n = happiness.length, j = 0; | ||
for (int i = n - 1; i >= n - k; --i) { | ||
happiness[i] = Math.max(happiness[i] - j++, 0); | ||
res += happiness[i]; | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if (root == null) | ||
return 0; | ||
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution { | ||
public boolean isSameTree(TreeNode p, TreeNode q) { | ||
if (p == null && q == null) | ||
return true; | ||
if (p == null || q == null) | ||
return false; | ||
if (p.val != q.val) | ||
return false; | ||
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public boolean isSubtree(TreeNode root, TreeNode subRoot) { | ||
if (root == null) | ||
return false; | ||
if (isSameTree(root, subRoot)) | ||
return true; | ||
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot); | ||
} | ||
|
||
Boolean isSameTree(TreeNode p, TreeNode q) { | ||
if (p == null && q == null) | ||
return true; | ||
if (p == null || q == null) | ||
return false; | ||
if (p.val != q.val) | ||
return false; | ||
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution { | ||
TreeNode prev = null; | ||
|
||
public boolean isValidBST(TreeNode root) { | ||
if (root == null) | ||
return true; | ||
if (!isValidBST(root.left)) | ||
return false; | ||
if (prev != null && prev.val >= root.val) | ||
return false; | ||
prev = root; | ||
return isValidBST(root.right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SELECT | ||
p.firstName, | ||
p.lastName, | ||
a.city, | ||
a.state | ||
FROM | ||
Person p | ||
LEFT JOIN | ||
Address a ON p.personId = a.personId; |