|
1 |
| -# Populating Next Right Pointers in Each Node |
2 |
| -You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: |
| 1 | +# Burst Balloons |
| 2 | +Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent. |
3 | 3 |
|
4 |
| -struct Node { |
5 |
| - int val; |
6 |
| - Node *left; |
7 |
| - Node *right; |
8 |
| - Node *next; |
9 |
| -} |
10 |
| -Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. |
| 4 | +Find the maximum coins you can collect by bursting the balloons wisely. |
11 | 5 |
|
12 |
| -Initially, all next pointers are set to NULL. |
| 6 | +Note: |
13 | 7 |
|
14 |
| - |
| 8 | +You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them. |
| 9 | +0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100 |
| 10 | +Example: |
15 | 11 |
|
16 |
| -Follow up: |
17 |
| - |
18 |
| -You may only use constant extra space. |
19 |
| -Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem. |
20 |
| - |
21 |
| - |
22 |
| -Example 1: |
23 |
| - |
24 |
| - |
25 |
| - |
26 |
| -Input: root = [1,2,3,4,5,6,7] |
27 |
| -Output: [1,#,2,3,#,4,5,6,7,#] |
28 |
| -Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level. |
29 |
| - |
30 |
| - |
31 |
| -Constraints: |
32 |
| - |
33 |
| -The number of nodes in the given tree is less than 4096. |
34 |
| --1000 <= node.val <= 1000<br> |
| 12 | +Input: [3,1,5,8] |
| 13 | +Output: 167 |
| 14 | +Explanation: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] |
| 15 | + coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167<br> |
35 | 16 |
|
36 | 17 | ## Idea
|
37 | 18 |
|
|
0 commit comments