thảo luận Leetcode mỗi ngày

JavaScript:
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;
    }
};
 
Last edited:
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
        }
    }
}
 
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
        }
    }
}
:love:
 
Nhìn AC xong hí hửng, đọc đề xong ko có ý tưởng gì luôn đệt mẹ :ah:
Bài này giải bằng backtracking thì đơn giản cơ mà constrain này thì ko backtracking được rồi
 
Nhìn AC xong hí hửng, đọc đề xong ko có ý tưởng gì luôn đệt mẹ :ah:
Bài này giải bằng backtracking thì đơn giản cơ mà constrain này thì ko backtracking được rồi
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ô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]
 
Ae cho mình hỏi sao code này với code này lại cho kết quả khác nhau nhỉ, chỗ mình bôi đậm cho kết quả khác nhau, cái 1 ra kết quả đúng cái 2 lại ra kết quả sai :ah:
Input
nums1 =
[72,97,8,32,15]
nums2 =
[63,97,57,60,83]
Ngồi cả tiếng ko biết sao nó sai cay thật :ah: cái chỗ khác nhau là chỗ current ...ấy
À thường phép toán sẽ được ưu tiên phép bitwise, phải để ý mới được ko bug sml
Python:
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)
Python:
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)
 
Last edited:
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ô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]
Hay quá fence, mình nghĩ ko ra xem solution luôn rồi :ah:
 
Java:
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];
    }
}

Bài này thấy tiệm cận hard rồi, mà accept rate cao quá
 
Back
Top