|
| 1 | +# [2479. Maximum XOR of Two Non-Overlapping Subtrees](https://leetcode.cn/problems/maximum-xor-of-two-non-overlapping-subtrees) |
| 2 | + |
| 3 | +[English Version](/solution/2400-2499/2479.Maximum%20XOR%20of%20Two%20Non-Overlapping%20Subtrees/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>There is an undirected tree with n nodes labeled from <code>0</code> to <code>n - 1</code>. You are given the integer n and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p> |
| 10 | + |
| 11 | +<p>Each node has an associated <strong>value</strong>. You are given an array <code>values</code> of length <code>n</code>, where <code>values[i]</code> is the <strong>value</strong> of the <code>i<sup>th</sup></code> node.</p> |
| 12 | + |
| 13 | +<p>Select any two <strong>non-overlapping</strong> subtrees. Your <strong>score</strong> is the bitwise XOR of the sum of the values within those subtrees.</p> |
| 14 | + |
| 15 | +<p>Return <em>the</em> <em><strong>maximum</strong></em> <i>possible <strong>score</strong> you can achieve</i>. <em>If it is impossible to find two nonoverlapping subtrees</em>, return <code>0</code>.</p> |
| 16 | + |
| 17 | +<p><strong>Note</strong> that:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>The <strong>subtree</strong> of a node is the tree consisting of that node and all of its descendants.</li> |
| 21 | + <li>Two subtrees are <strong>non-overlapping </strong>if they do not share <strong>any common</strong> node.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | +<p><strong class="example">Example 1:</strong></p> |
| 26 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2479.Maximum%20XOR%20of%20Two%20Non-Overlapping%20Subtrees/images/treemaxxor.png" style="width: 346px; height: 249px;" /> |
| 27 | +<pre> |
| 28 | +<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,3],[1,4],[2,5]], values = [2,8,3,6,2,5] |
| 29 | +<strong>Output:</strong> 24 |
| 30 | +<strong>Explanation:</strong> Node 1's subtree has sum of values 16, while node 2's subtree has sum of values 8, so choosing these nodes will yield a score of 16 XOR 8 = 24. It can be proved that is the maximum possible score we can obtain. |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | +<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2400-2499/2479.Maximum%20XOR%20of%20Two%20Non-Overlapping%20Subtrees/images/tree3drawio.png" style="width: 240px; height: 261px;" /> |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> n = 3, edges = [[0,1],[1,2]], values = [4,6,1] |
| 37 | +<strong>Output:</strong> 0 |
| 38 | +<strong>Explanation:</strong> There is no possible way to select two non-overlapping subtrees, so we just return 0. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Constraints:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li><code>2 <= n <= 5 * 10<sup>4</sup></code></li> |
| 46 | + <li><code>edges.length == n - 1</code></li> |
| 47 | + <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li> |
| 48 | + <li><code>values.length == n</code></li> |
| 49 | + <li><code>1 <= values[i] <= 10<sup>9</sup></code></li> |
| 50 | + <li>It is guaranteed that <code>edges</code> represents a valid tree.</li> |
| 51 | +</ul> |
| 52 | + |
| 53 | +## 解法 |
| 54 | + |
| 55 | +<!-- 这里可写通用的实现逻辑 --> |
| 56 | + |
| 57 | +<!-- tabs:start --> |
| 58 | + |
| 59 | +### **Python3** |
| 60 | + |
| 61 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 62 | + |
| 63 | +```python |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +### **Java** |
| 68 | + |
| 69 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 70 | + |
| 71 | +```java |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +### **C++** |
| 76 | + |
| 77 | +```cpp |
| 78 | + |
| 79 | +``` |
| 80 | + |
| 81 | +### **Go** |
| 82 | + |
| 83 | +```go |
| 84 | + |
| 85 | +``` |
| 86 | + |
| 87 | +### **...** |
| 88 | + |
| 89 | +``` |
| 90 | +
|
| 91 | +``` |
| 92 | + |
| 93 | +<!-- tabs:end --> |
0 commit comments