TBNv2
Senior Member
code này bị leak mem mà.10 năm rồi chưa code C++, viết vầy không rõ có bị leak không
code này bị leak mem mà.10 năm rồi chưa code C++, viết vầy không rõ có bị leak không
var removeLeafNodes = function(root, target) {
function recursion(node) {
if (!node) return false;
if (node.left && recursion(node.left)) node.left = null;
if (node.right && recursion(node.right)) node.right = null;
return !node.left && !node.right && node.val === target;
}
if (recursion(root)) {
return null;
}
return root;
};
Code:class Solution { public: TreeNode* removeLeafNodes(TreeNode*& root, int target) { if (!root) return root; removeLeafNodes(root->left, target); removeLeafNodes(root->right, target); if (!root->left && !root->right && root->val == target) { root = nullptr; } return root; } };
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun removeLeafNodes(root: TreeNode?, target: Int): TreeNode? {
if (root == null) {
return null
}
root.left = removeLeafNodes(root.left, target)
root.right = removeLeafNodes(root.right, target)
return if (root.left == null && root.right == null && root.`val` == target) {
null
} else {
root
}
}
}
C-like:/** * Example: * var ti = TreeNode(5) * var v = ti.`val` * Definition for a binary tree node. * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ class Solution { fun removeLeafNodes(root: TreeNode?, target: Int): TreeNode? { if (root == null) { return null } root.left = removeLeafNodes(root.left, target) root.right = removeLeafNodes(root.right, target) return if (root.left == null && root.right == null && root.`val` == target) { null } else { root } } }
Mỗi node 1 coin nên chỉ cần so sánh số coin nó hold với số nodes của mỗi sub tree là ra được số lượng move đến root của sub tree đó. Làm tương tự, đệ quy với các subtree nhỏ hơn. => Bài này chỉ cần viết đệ quy bt thôiNhìn AC xong hí hửng, đọc đề xong ko có ý tưởng gì luôn đệt mẹ
Bài này giải bằng backtracking thì đơn giản cơ mà constrain này thì ko backtracking được rồi
class Solution:
def distributeCoins(self, root: Optional[TreeNode]) -> int:
def dfs(node):
if not node:
return 0,0,0
l_num_nodes, l_num_coins, l_num_move = dfs(node.left)
r_num_nodes, r_num_coins, r_num_move = dfs(node.right)
num_nodes = l_num_nodes + r_num_nodes + 1
num_coins = l_num_coins + r_num_coins + node.val
num_move = abs(l_num_nodes - l_num_coins) + abs(r_num_nodes - r_num_coins) + l_num_move + r_num_move
return num_nodes, num_coins, num_move
return dfs(root)[2]
class Solution:
def minimumXORSum(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
@lru_cache(None)
def dp(i, mask):
if i == n:
return 0
ans = inf
for j in range(n):
if (mask >> j) & 1 == 0:
current = nums1[i]^nums2[j]
ans = min(ans, current + dp(i + 1, mask|(1 << j)))
return ans
return dp(0, 0)
class Solution:
def minimumXORSum(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
@lru_cache(None)
def dp(i, mask):
if i == n:
return 0
ans = inf
for j in range(n):
if (mask >> j) & 1 == 0:
ans = min(ans, nums1[i]^nums2[j] + dp(i + 1, mask|(1 << j)))
return ans
return dp(0, 0)
Hay quá fence, mình nghĩ ko ra xem solution luôn rồiMỗi node 1 coin nên chỉ cần so sánh số coin nó hold với số nodes của mỗi sub tree là ra được số lượng move đến root của sub tree đó. Làm tương tự, đệ quy với các subtree nhỏ hơn. => Bài này chỉ cần viết đệ quy bt thôi
Python:class Solution: def distributeCoins(self, root: Optional[TreeNode]) -> int: def dfs(node): if not node: return 0,0,0 l_num_nodes, l_num_coins, l_num_move = dfs(node.left) r_num_nodes, r_num_coins, r_num_move = dfs(node.right) num_nodes = l_num_nodes + r_num_nodes + 1 num_coins = l_num_coins + r_num_coins + node.val num_move = abs(l_num_nodes - l_num_coins) + abs(r_num_nodes - r_num_coins) + l_num_move + r_num_move return num_nodes, num_coins, num_move return dfs(root)[2]
class Solution {
public int[] divideAndConquer(TreeNode root) {
if (root == null)
return new int[]{0, 1};
int[] lt = divideAndConquer(root.left);
int[] rt = divideAndConquer(root.right);
int[] res = new int[2];
int leftVal = root.left == null ? 1 : root.left.val;
int rightVal = root.right == null ? 1 : root.right.val;
res[0] = lt[0] + rt[0] + Math.abs(lt[1] - leftVal) + Math.abs(rt[1] - rightVal);
res[1] = lt[1] + rt[1] - leftVal - rightVal + 1;
return res;
}
public int distributeCoins(TreeNode root) {
int[] res = divideAndConquer(root);
return res[0];
}
}