Maximum XOR of Two Non-Overlapping Subtrees — LeetCode 2479 Python Solution
- Problem
- #2479
- Pattern
- Trie
- Reading time
- 10 min
- Source
- leetcode.com
The problem
There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
Example
- Input
- n = 6, edges = [[0,1],[0,2],[1,3],[1,4],[2,5]], values = [2,8,3,6,2,5]
- Output
- 24
- Explanation
- 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.
Python solution
class Trie:
def __init__(self):
self.children = [None] * 2
def insert(self, x):
node = self
for i in range(47, -1, -1):
v = (x >> i) & 1
if node.children[v] is None:
node.children[v] = Trie()
node = node.children[v]
def search(self, x):
node = self
res = 0
for i in range(47, -1, -1):
v = (x >> i) & 1
if node is None:
return res
if node.children[v ^ 1]:
res = res << 1 | 1
node = node.children[v ^ 1]
else:
res <<= 1
node = node.children[v]
return res
class Solution:
def maxXor(self, n: int, edges: List[List[int]], values: List[int]) -> int:
def dfs1(i, fa):
t = values[i]
for j in g[i]:
if j != fa:
t += dfs1(j, i)
s[i] = t
return t
def dfs2(i, fa):
nonlocal ans
ans = max(ans, tree.search(s[i]))
for j in g[i]:
if j != fa:
dfs2(j, i)
tree.insert(s[i])
g = defaultdict(list)
for a, b in edges:
g[a].append(b)
g[b].append(a)
s = [0] * n
dfs1(0, -1)
ans = 0
tree = Trie()
dfs2(0, -1)
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(V+E) |
| Space | O(V) auxiliary |
Pattern: Trie
Store a set of words by their shared prefixes so lookups cost the length of the word. LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees is filed here because LeetCode tags it Trie, which is the vocabulary this hub collects.
The trie guide has the Python template for the pattern and the 49 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees?
- LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees is rated Hard on LeetCode.
- What is the time complexity of LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees?
- The Python solution on this page runs in O(V+E).
- What is the space complexity of LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees?
- The Python solution on this page uses O(V) auxiliary space.
- What topics does LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees cover?
- LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees is tagged Tree, Depth-First Search, Graph and Trie on LeetCode.
- Is LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees a premium problem?
- Yes. LeetCode 2479. Maximum XOR of Two Non-Overlapping Subtrees is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.