[LeetCode] 124. Binary Tree Maximum Path Sum

124. Binary Tree Maximum Path Sum Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: Dynamic Programming、Tree、Depth-First Search、Binary Tree 一、題目 A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node’s values in the path. Given the root of a binary tree, return the maximum path sum of any non-empty path. Example 1: ...

December 11, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 1339. Maximum Product of Splitted Binary Tree

1339. Maximum Product of Splitted Binary Tree Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Tree、Depth-First Search、Binary Tree 一、題目 Given the root of a binary tree, split the bianry tree into two subtrees by removing one edge such that the product of the sums of the subtreesis maximized. Return the maximum product of the sums of the two subtrees. Since the answer may be too large, return it modulo 10^9 + 7. Note that you need to maximize the answer before taking the mod and not after taking it. ...

December 10, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 1026. Maximum Difference Between Node and Ancestor

1026. Maximum Difference Between Node and Ancestor Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Tree、Depth-First Search、Binary Tree 一、題目 Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b. A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b. ...

December 10, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 872. Leaf-Similar Trees

872. Leaf-Similar Trees Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Tree、Depth-First Search、Binary Tree 一、題目 Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence. For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8). Two binary trees are considered leaf-similar if their leaf value sequence is the same. Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar. ...

December 8, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 938. Range Sum of BST

938. Range Sum of BST Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Tree、Depth-First Search、Binary Search Tree、Binary Tree 一、題目 Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. Example 1: Input: root = [10,5,15,3,7,null,18], low = 7, high = 15 Output: 32 Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32. Example 2: ...

December 7, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 328. Odd Even Linked List

328. Odd Even Linked List Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Linked List 一、題目 Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the problem in O(1) extra space complexity and O(n) time complexity. ...

December 6, 2022 · 1 分鐘 · Rain Hu

[LeetCode] 2472. Maximum Number of Non-overlapping Palindrome Substrings

2472. Maximum Number of Non-overlapping Palindrome Substrings Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: String、Dynamic Programming \(\color{blue}\textsf{weekly Contest 319}\) 一、題目 You are given a string s and a positive integer k. Select a set of non-overlapping substrings from the string s that satisfy the following conditions: The length of each substring is at least k. Each substring is a palindrome. Return the maximum number of substrings in an optimal selection. A substring is a contiguous sequence of characters within a string. Example 1: ...

November 29, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2471. Minimum Number of Operations to Sort a Binary Tree by Level

2471. Minimum Number of Operations to Sort a Binary Tree by Level Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Tree、Breadth-First Search、Binary Tree \(\color{blue}\textsf{weekly Contest 319}\) 一、題目 You are given the root of a binary tree with unique values. In one operation, you can choose any two nodes at the same level and swap their values. Return the minimum number of operations needed to make the values at each level sorted in a strictly increasing order. The level of a node is the number of edges along the path between it and the root node. ...

November 29, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2470. Number of Subarrays With LCM Equal to K

2470. Number of Subarrays With LCM Equal to K Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Math、Number Theory \(\color{blue}\textsf{weekly Contest 319}\) 一、題目 Given an integer array nums and an integer k, return the number of subarrays of nums where the least common multiple of the subarray’s elements is k. A subarray is a contiguous non-empty sequence of elements within an array. The least common multiple of an array is the smallest positive integer that is divisible by all the array elements. ...

November 29, 2022 · 2 分鐘 · Rain Hu

[LeetCode] 2469. Convert the Temperature

2469. Convert the Temperature Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Math \(\color{blue}\textsf{weekly Contest 319}\) 一、題目 You are given a non-negative floating point number rounded to two decimal places celsius, that denotes the temperature in Celsius. You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit]. Return the array ans. Answers within 10^-5 of the actual answer will be accepted. Note that: Kelvin = Celsius + 273.15 Fahrenheit = Celsius * 1.80 + 32.00 Example 1: ...

November 29, 2022 · 1 分鐘 · Rain Hu