Skip to content

Commit 9b8e04f

Browse files
committed
new soln
1 parent c931811 commit 9b8e04f

6 files changed

+243
-0
lines changed

100.IsSameTree.cs

+13
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,17 @@ public bool IsSameTree(TreeNode p, TreeNode q) {
4040
return false;
4141
return IsSameTree(p.left,q.left) && IsSameTree(p.right,q.right);
4242
}
43+
}
44+
45+
public class Solution {
46+
public bool IsSameTree(TreeNode p, TreeNode q) {
47+
if(p == null && q == null)
48+
return true;
49+
else if(p == null || q == null)
50+
return false;
51+
else if(p.val == q.val)
52+
return IsSameTree(p.left,q.left) && IsSameTree(p.right,q.right);
53+
else
54+
return false;
55+
}
4356
}

1061.SmallestEquivalentString.cs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// 1061. Lexicographically Smallest Equivalent String
2+
// You are given two strings of the same length s1 and s2 and a string baseStr.
3+
// We say s1[i] and s2[i] are equivalent characters.
4+
// For example, if s1 = "abc" and s2 = "cde", then we have 'a' == 'c', 'b' == 'd', and 'c' == 'e'.
5+
// Equivalent characters follow the usual rules of any equivalence relation:
6+
// Reflexivity: 'a' == 'a'.
7+
// Symmetry: 'a' == 'b' implies 'b' == 'a'.
8+
// Transitivity: 'a' == 'b' and 'b' == 'c' implies 'a' == 'c'.
9+
// For example, given the equivalency information from s1 = "abc" and s2 = "cde", "acd" and "aab" are equivalent strings of baseStr = "eed", and "aab" is the lexicographically smallest equivalent string of baseStr.
10+
// Return the lexicographically smallest equivalent string of baseStr by using the equivalency information from s1 and s2.
11+
// Example 1:
12+
// Input: s1 = "parker", s2 = "morris", baseStr = "parser"
13+
// Output: "makkek"
14+
// Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
15+
// The characters in each group are equivalent and sorted in lexicographical order.
16+
// So the answer is "makkek".
17+
// Example 2:
18+
// Input: s1 = "hello", s2 = "world", baseStr = "hold"
19+
// Output: "hdld"
20+
// Explanation: Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
21+
// So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
22+
// Example 3:
23+
// Input: s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
24+
// Output: "aauaaaaada"
25+
// Explanation: We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
26+
// Constraints:
27+
// 1 <= s1.length, s2.length, baseStr <= 1000
28+
// s1.length == s2.length
29+
// s1, s2, and baseStr consist of lowercase English letters.
30+
31+
public class Solution {
32+
int[] parent = new int[26];
33+
public string SmallestEquivalentString(string s1, string s2, string baseStr) {
34+
Array.Fill(parent,-1);
35+
for(int i = 0; i < s1.Length; i++)
36+
Union(s1[i] - 'a', s2[i] - 'a');
37+
string res = "";
38+
for(int i = 0; i < baseStr.Length; i++){
39+
char ch = (char)('a' + Find(baseStr[i] - 'a'));
40+
res += ch;
41+
}
42+
return res;
43+
}
44+
int Find(int ch){
45+
if(parent[ch] == -1)
46+
return ch;
47+
return parent[ch] = Find(parent[ch]);
48+
}
49+
void Union(int ch1, int ch2){
50+
int p1 = Find(ch1);
51+
int p2 = Find(ch2);
52+
53+
if(p1 != p2)
54+
parent[Math.Max(p1,p2)] = Math.Min(p1,p2);
55+
}
56+
}

144.PreorderTraversal.cs

+16
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,20 @@ public void Preorder(TreeNode root, IList<int> result){
3737
if(root.right != null)
3838
Preorder(root.right, result);
3939
}
40+
}
41+
42+
43+
public class Solution {
44+
public IList<int> PreorderTraversal(TreeNode root) {
45+
IList<int> res = new List<int>();
46+
Preorder(root, res);
47+
return res;
48+
}
49+
public void Preorder(TreeNode root, IList<int> res){
50+
if(root == null)
51+
return;
52+
res.Add(root.val);
53+
Preorder(root.left, res);
54+
Preorder(root.right, res);
55+
}
4056
}

1443.MinTime.cs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 1443. Minimum Time to Collect All Apples in a Tree
2+
// Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at vertex 0 and coming back to this vertex.
3+
// The edges of the undirected tree are given in the array edges, where edges[i] = [ai, bi] means that exists an edge connecting the vertices ai and bi. Additionally, there is a boolean array hasApple, where hasApple[i] = true means that vertex i has an apple; otherwise, it does not have any apple.
4+
// Example 1:
5+
// Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]
6+
// Output: 8
7+
// Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.
8+
// Example 2:
9+
// Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]
10+
// Output: 6
11+
// Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.
12+
// Example 3:
13+
// Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false]
14+
// Output: 0
15+
// Constraints:
16+
// 1 <= n <= 105
17+
// edges.length == n - 1
18+
// edges[i].length == 2
19+
// 0 <= ai < bi <= n - 1
20+
// hasApple.length == n
21+
22+
public class Solution {
23+
public int MinTime(int n, int[][] edges, IList<bool> hasApple) {
24+
List<List<int>> adj = new List<List<int>>();
25+
for(int i = 0; i < n; i++){
26+
adj.Add(new List<int>());
27+
}
28+
foreach(var edge in edges){
29+
adj[edge[0]].Add(edge[1]);
30+
adj[edge[1]].Add(edge[0]);
31+
}
32+
return MinTimeToCollectApples(0, adj, hasApple, 0);
33+
}
34+
// Helper function
35+
int MinTimeToCollectApples(int index, List<List<int>> adj,
36+
IList<bool> hasApple, int parent){
37+
int total = 0;
38+
foreach(var nbr in adj[index]){
39+
if (nbr == parent)
40+
continue;
41+
total += MinTimeToCollectApples(nbr, adj, hasApple, index);
42+
}
43+
if( index != 0 && (hasApple[index] || total > 0))
44+
total += 2;
45+
return total;
46+
}
47+
}

1519.CountSubTrees.cs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// 1519. Number of Nodes in the Sub-Tree With the Same Label
2+
// You are given a tree (i.e. a connected, undirected graph that has no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. The root of the tree is the node 0, and each node of the tree has a label which is a lower-case character given in the string labels (i.e. The node with the number i has the label labels[i]).
3+
// The edges array is given on the form edges[i] = [ai, bi], which means there is an edge between nodes ai and bi in the tree.
4+
// Return an array of size n where ans[i] is the number of nodes in the subtree of the ith node which have the same label as node i.
5+
// A subtree of a tree T is the tree consisting of a node in T and all of its descendant nodes.
6+
// Example 1:
7+
// Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], labels = "abaedcd"
8+
// Output: [2,1,1,1,1,1,1]
9+
// Explanation: Node 0 has label 'a' and its sub-tree has node 2 with label 'a' as well, thus the answer is 2. Notice that any node is part of its sub-tree.
10+
// Node 1 has a label 'b'. The sub-tree of node 1 contains nodes 1,4 and 5, as nodes 4 and 5 have different labels than node 1, the answer is just 1 (the node itself).
11+
// Example 2:
12+
// Input: n = 4, edges = [[0,1],[1,2],[0,3]], labels = "bbbb"
13+
// Output: [4,2,1,1]
14+
// Explanation: The sub-tree of node 2 contains only node 2, so the answer is 1.
15+
// The sub-tree of node 3 contains only node 3, so the answer is 1.
16+
// The sub-tree of node 1 contains nodes 1 and 2, both have label 'b', thus the answer is 2.
17+
// The sub-tree of node 0 contains nodes 0, 1, 2 and 3, all with label 'b', thus the answer is 4.
18+
// Example 3:
19+
// Input: n = 5, edges = [[0,1],[0,2],[1,3],[0,4]], labels = "aabab"
20+
// Output: [3,2,1,1,1]
21+
// Constraints:
22+
// 1 <= n <= 105
23+
// edges.length == n - 1
24+
// edges[i].length == 2
25+
// 0 <= ai, bi < n
26+
// ai != bi
27+
// labels.length == n
28+
// labels is consisting of only of lowercase English letters.
29+
30+
public class Solution {
31+
public int[] CountSubTrees(int n, int[][] edges, string labels) {
32+
int[] res = new int[n];
33+
List<List<int>> adj = new List<List<int>>();
34+
for (int i = 0; i < n; i++)
35+
adj.Add(new List<int>());
36+
foreach (var edge in edges)
37+
{
38+
adj[edge[0]].Add(edge[1]);
39+
adj[edge[1]].Add(edge[0]);
40+
}
41+
DFS(0, -1, adj, labels, res);
42+
return res;
43+
}
44+
int[] DFS(int idx, int parent, List<List<int>> adj,
45+
string labels, int[] res)
46+
{
47+
int[] freq = new int[26];
48+
freq[labels[idx] - 'a'] = 1;
49+
foreach (var nbr in adj[idx])
50+
{
51+
if (nbr != parent)
52+
{
53+
int[] temp = DFS(nbr, idx, adj, labels, res);
54+
for(int i = 0; i < 26; i++)
55+
freq[i] += temp[i];
56+
}
57+
}
58+
res[idx] = freq[labels[idx] - 'a'];
59+
return freq;
60+
}
61+
}

2246.LongestPath.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 2246. Longest Path With Different Adjacent Characters
2+
// You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i. Since node 0 is the root, parent[0] == -1.
3+
// You are also given a string s of length n, where s[i] is the character assigned to node i.
4+
// Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same character assigned to them.
5+
// Example 1:
6+
// Input: parent = [-1,0,0,1,1,2], s = "abacbe"
7+
// Output: 3
8+
// Explanation: The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -> 1 -> 3. The length of this path is 3, so 3 is returned.
9+
// It can be proven that there is no longer path that satisfies the conditions.
10+
// Example 2:
11+
// Input: parent = [-1,0,0,0], s = "aabc"
12+
// Output: 3
13+
// Explanation: The longest path where each two adjacent nodes have different characters is the path: 2 -> 0 -> 3. The length of this path is 3, so 3 is returned.
14+
// Constraints:
15+
// n == parent.length == s.length
16+
// 1 <= n <= 105
17+
// 0 <= parent[i] <= n - 1 for all i >= 1
18+
// parent[0] == -1
19+
// parent represents a valid tree.
20+
// s consists of only lowercase English letters.
21+
22+
public class Solution {
23+
int max = 0;
24+
public int LongestPath(int[] parent, string s) {
25+
List<List<int>> adj = new();
26+
for(int i = 0; i < parent.Length; i++)
27+
adj.Add(new List<int>());
28+
for(int i = 1; i < parent.Length; i++){
29+
adj[parent[i]].Add(i);
30+
}
31+
Traverse(s,adj,0);
32+
return max;
33+
}
34+
int Traverse(string s, List<List<int>> adj, int idx){
35+
int ch1Len = 0, ch2Len = 0;
36+
foreach(var nbr in adj[idx]){
37+
int len = Traverse(s,adj,nbr);
38+
max = Math.Max(len,max);
39+
if(s[idx] == s[nbr]) continue;
40+
if(len > ch1Len){
41+
ch2Len = ch1Len;
42+
ch1Len = len;
43+
}
44+
else
45+
ch2Len = Math.Max(len,ch2Len);
46+
}
47+
max = Math.Max(max, 1+ch1Len+ch2Len);
48+
return 1 + ch1Len;
49+
}
50+
}

0 commit comments

Comments
 (0)