Skip to content

Commit 2e16ee9

Browse files
committed
Commit 09/05
1 parent 2a7bce4 commit 2e16ee9

5 files changed

Lines changed: 177 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ Feel free to add issues, comment and pull request.
6565
| Leetcode | [653. Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/) | [Java](./java/Twosum.java) \| [Python](./Python/) | _O(n)_ | _O(n)_ | Easy | |
6666
| Leetcode | [617. Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Java](./java/mergeTrees.java) \| [Python](./Python/) | _O(mn)_ | _O(1)_ | Easy | |
6767
| Leetcode | [637. Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) | [Java](./java/averageOfLevels.java) \| [Python](./Python/) | _O(n)_ | _O(1)_ | Easy | |
68+
| Leetcode | [663. Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/description/) | [Java](./java/CheckEqualTrees.java) \| [Python](./Python/) | | | Medium | |
69+
| Leetcode | [623. Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/description/) | [Java](./java/addOneRow.java) \| [Python](./Python/) | | | Medium | |
6870

6971

7072
## Dynamic Programming

java/CheckEqualTrees.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean checkEqualTree(TreeNode root) {
12+
13+
if(root == null)
14+
return false;
15+
16+
int sum = checkSum(root);
17+
18+
if(sum %2 != 0)
19+
return false;
20+
21+
Stack<TreeNode> stack = new Stack<>();
22+
stack.push(root);
23+
24+
while(!stack.isEmpty())
25+
{
26+
TreeNode node = stack.pop();
27+
28+
if(node.left != null)
29+
{
30+
int leftSum = checkSum(node.left);
31+
if(leftSum == sum - leftSum)
32+
return true;
33+
stack.push(node.left);
34+
}
35+
36+
if(node.right != null)
37+
{
38+
int rightSum = checkSum(node.right);
39+
if(rightSum == sum - rightSum)
40+
return true;
41+
42+
stack.push(node.right);
43+
}
44+
45+
}
46+
return false;
47+
48+
}
49+
50+
public int checkSum(TreeNode root)
51+
{
52+
if(root == null)
53+
return 0;
54+
55+
return root.val + checkSum(root.left) + checkSum(root.right);
56+
}
57+
58+
}

java/addOneRow.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode addOneRow(TreeNode root, int v, int d) {
12+
13+
if(d == 1)
14+
{
15+
TreeNode result = new TreeNode(v);
16+
result.left = root;
17+
return result;
18+
}
19+
20+
dfs(root, 1, v, d);
21+
return root;
22+
}
23+
24+
public void dfs(TreeNode root, int depth, int v, int d)
25+
{
26+
if(root == null)
27+
return;
28+
29+
if(depth < d-1)
30+
{
31+
dfs(root.left, depth+1, v, d);
32+
dfs(root.right, depth+1, v, d);
33+
}
34+
else
35+
{
36+
TreeNode result = root.left;
37+
root.left = new TreeNode(v);
38+
root.left.left = result;
39+
result = root.right;
40+
root.right = new TreeNode(v);
41+
root.right.right = result;
42+
43+
44+
}
45+
}
46+
}

java/leastInterval.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public int leastInterval(char[] tasks, int n) {
3+
4+
int[] map = new int[26];
5+
6+
for(char task : tasks)
7+
map[task - 'A']++;
8+
9+
PriorityQueue<Integer> queue = new PriorityQueue<>(26, Collections.reverseOrder());
10+
11+
for(int entry : map)
12+
if (entry > 0)
13+
queue.offer(entry);
14+
15+
int time = 0;
16+
17+
while(!queue.isEmpty())
18+
{
19+
int i = 0;
20+
List<Integer> temp = new ArrayList<>();
21+
while(i <= n)
22+
{
23+
if(!queue.isEmpty())
24+
{
25+
if(queue.peek()>1)
26+
{
27+
temp.add(queue.poll()-1);
28+
}
29+
else
30+
queue.poll();
31+
32+
33+
}
34+
time++;
35+
if(queue.isEmpty() && temp.size()==0)
36+
break;
37+
i++;
38+
}
39+
40+
for(int a : temp)
41+
queue.offer(a);
42+
}
43+
44+
return time;
45+
46+
}
47+
}

java/triangleNumber.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public int triangleNumber(int[] nums) {
3+
if(nums.length == 0)
4+
return 0;
5+
6+
Arrays.sort(nums);
7+
int count = 0;
8+
for(int i=2; i<nums.length; i++)
9+
{
10+
int j = 0, k = i-1;
11+
while(j < k)
12+
{
13+
if(nums[j]+nums[k] > nums[i])
14+
{
15+
count += k-j;
16+
k--;
17+
}
18+
else j++;
19+
20+
}
21+
}
22+
return count;
23+
}
24+
}

0 commit comments

Comments
 (0)