Skip to content

Commit fd30bff

Browse files
committed
new soln
1 parent 1128e0c commit fd30bff

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

78.Subsets.cs

+18
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,22 @@ public IList<IList<int>> Subsets(int[] nums) {
2828
}
2929
return result;
3030
}
31+
}
32+
33+
public class Solution {
34+
public IList<IList<int>> Subsets(int[] nums) {
35+
IList<IList<int>> result = new List<IList<int>>();
36+
Subsets(nums, 0, result, new List<int>());
37+
return result;
38+
}
39+
private void Subsets(int[] nums, int itr, IList<IList<int>> result, IList<int> set){
40+
if(itr == nums.Length){
41+
result.Add(new List<int>(set));
42+
return;
43+
}
44+
Subsets(nums, itr+1, result, set);
45+
set.Add(nums[itr]);
46+
Subsets(nums, itr+1, result, set);
47+
set.RemoveAt(set.Count -1);
48+
}
3149
}

979.DistributeCoins.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 979. Distribute Coins in Binary Tree
2+
// You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree.
3+
// In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent.
4+
// Return the minimum number of moves required to make every node have exactly one coin.
5+
6+
// Example 1:
7+
// Input: root = [3,0,0]
8+
// Output: 2
9+
// Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
10+
// Example 2:
11+
// Input: root = [0,3,0]
12+
// Output: 3
13+
// Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
14+
15+
// Constraints:
16+
17+
// The number of nodes in the tree is n.
18+
// 1 <= n <= 100
19+
// 0 <= Node.val <= n
20+
// The sum of all Node.val is n.
21+
22+
/**
23+
* Definition for a binary tree node.
24+
* public class TreeNode {
25+
* public int val;
26+
* public TreeNode left;
27+
* public TreeNode right;
28+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
29+
* this.val = val;
30+
* this.left = left;
31+
* this.right = right;
32+
* }
33+
* }
34+
*/
35+
public class Solution {
36+
public int DistributeCoins(TreeNode root) {
37+
return DistributeCoins(root, null);
38+
}
39+
40+
public int DistributeCoins(TreeNode root, TreeNode parent) {
41+
if(root == null)
42+
return 0;
43+
int moves = DistributeCoins(root.left, root) + DistributeCoins(root.right, root);
44+
int x = root.val -1;
45+
if(parent != null)
46+
parent.val += x;
47+
moves += Math.Abs(x);
48+
return moves;
49+
}
50+
}

0 commit comments

Comments
 (0)