1130:叶值的最小代价生成树
This commit is contained in:
parent
0995757c72
commit
6f0e01e7d8
@ -0,0 +1,71 @@
|
|||||||
|
//给你一个正整数数组 arr,考虑所有满足以下条件的二叉树:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 每个节点都有 0 个或是 2 个子节点。
|
||||||
|
// 数组 arr 中的值与树的中序遍历中每个叶节点的值一一对应。(知识回顾:如果一个节点有 0 个子节点,那么该节点为叶节点。)
|
||||||
|
// 每个非叶节点的值等于其左子树和右子树中叶节点的最大值的乘积。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 在所有这样的二叉树中,返回每个非叶节点的值的最小可能总和。这个和的值是一个 32 位整数。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例:
|
||||||
|
//
|
||||||
|
// 输入:arr = [6,2,4]
|
||||||
|
//输出:32
|
||||||
|
//解释:
|
||||||
|
//有两种可能的树,第一种的非叶节点的总和为 36,第二种非叶节点的总和为 32。
|
||||||
|
//
|
||||||
|
// 24 24
|
||||||
|
// / \ / \
|
||||||
|
// 12 4 6 8
|
||||||
|
// / \ / \
|
||||||
|
//6 2 2 4
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 2 <= arr.length <= 40
|
||||||
|
// 1 <= arr[i] <= 15
|
||||||
|
// 答案保证是一个 32 位带符号整数,即小于 2^31。
|
||||||
|
//
|
||||||
|
// Related Topics 栈 树 动态规划
|
||||||
|
// 👍 163 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
//1130:叶值的最小代价生成树
|
||||||
|
public class MinimumCostTreeFromLeafValues {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new MinimumCostTreeFromLeafValues().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int mctFromLeafValues(int[] arr) {
|
||||||
|
Stack<Integer> stack = new Stack<>();
|
||||||
|
int result = 0;
|
||||||
|
stack.push(Integer.MAX_VALUE);
|
||||||
|
for (int num : arr) {
|
||||||
|
while (num > stack.peek()) {
|
||||||
|
result += stack.pop() * Math.min(stack.peek(), num);
|
||||||
|
}
|
||||||
|
stack.push(num);
|
||||||
|
}
|
||||||
|
while (stack.size() > 2) {
|
||||||
|
result += stack.pop() * stack.peek();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
<p>给你一个正整数数组 <code>arr</code>,考虑所有满足以下条件的二叉树:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>每个节点都有 0 个或是 2 个子节点。</li>
|
||||||
|
<li>数组 <code>arr</code> 中的值与树的中序遍历中每个叶节点的值一一对应。(知识回顾:如果一个节点有 0 个子节点,那么该节点为叶节点。)</li>
|
||||||
|
<li>每个非叶节点的值等于其左子树和右子树中叶节点的最大值的乘积。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>在所有这样的二叉树中,返回每个非叶节点的值的最小可能总和。这个和的值是一个 32 位整数。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>arr = [6,2,4]
|
||||||
|
<strong>输出:</strong>32
|
||||||
|
<strong>解释:</strong>
|
||||||
|
有两种可能的树,第一种的非叶节点的总和为 36,第二种非叶节点的总和为 32。
|
||||||
|
|
||||||
|
24 24
|
||||||
|
/ \ / \
|
||||||
|
12 4 6 8
|
||||||
|
/ \ / \
|
||||||
|
6 2 2 4</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>2 <= arr.length <= 40</code></li>
|
||||||
|
<li><code>1 <= arr[i] <= 15</code></li>
|
||||||
|
<li>答案保证是一个 32 位带符号整数,即小于 <code>2^31</code>。</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>栈</li><li>树</li><li>动态规划</li></div></div>\n<div><li>👍 163</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user