<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class Node:
    def __init__(self, val, left=None, right=None, isNum=False):
        self.val = val
        self.left = left
        self.right = right
        self.isNum = isNum

def insert(s, curIdx):
    if curIdx == len(s):
        return None
    res = Node(ord(s[curIdx]) - ord('0'))
    if curIdx+1 == len(s):
        res.isNum = True
        return res
    elif s[curIdx+1] == '0':
        res.left = insert(s, curIdx+1)
    else:
        res.right = insert(s, curIdx+1)
    return res


def convertToReverseBinaryString(num):
    res = ""
    for i in range(32):
        if num % 2 == 1:
            res += "1"
        else:
            res += "0"
        num = int(num/2)
    res += '0' # extra bit to have a starting node
    return res[::-1]
def findBest(val, root1, root2):
    mx = val
    usedBest = False
    if root1.left is not None and root2.right is not None:
        mx = max(mx, findBest(val*2+1, root1.left, root2.right))
        usedBest = True
    if root1.right is not None and root2.left is not None:
        mx = max(mx, findBest(val*2+1, root1.right, root2.left))
        usedBest = True
    if not usedBest:
        if root1.left is not None and root2.left is not None:
            mx = max(mx, findBest(val*2, root1.left, root2.left))
        if root1.right is not None and root2.right is not None:
            mx = max(mx, findBest(val*2, root1.right, root2.right))
    return mx
    
class Solution:
    def findMaximumXOR(self, nums: List[int]) -> int:
        root = Node(0,None,None,False)
        for num in nums:
            s = convertToReverseBinaryString(num)
            curIdx = 0
            cur = root
            while curIdx+1 < len(s):
                curIdx += 1
                if s[curIdx] == '0':
                    if cur.left is not None:
                        cur = cur.left
                    else:
                        cur.left = insert(s, curIdx)
                        break
                else:
                    if cur.right is not None:
                        cur = cur.right
                    else:
                        cur.right = insert(s, curIdx)
                        break
        # find first division
        while root.left is None or root.right is None:
            if root.left is None and root.right is None:
                return 0
            elif root.left is None:
                root = root.right
            else:
                root = root.left
        return findBest(1, root.left, root.right)
235621