<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 TreeNode:     # we will be creating tree.
    zero=None
    one=None
    value=None

class Solution:
    def findMaximumXOR(self, nums: List[int]) -> int:
        
        # we'll first construct a tree that represents all the nums. 
        root=TreeNode()
        bit=range(31,-1,-1) # [31, 30, .... 0]. We'll use this to iterate over num bit by bit.
        
        for num in nums:
            curr=root       # always start from top of the tree
            for i in bit:
                temp=1<<i       # temp will be something like 000000010000000, 1 being at the i'th position
                check=temp&num  # this is check bit. if it's 0 means num bit is 0.
                if check is 0:  # so num bit is zero. So we will add node zero in the tree.
                    if not curr.zero:   
                        curr.zero=TreeNode()
                    curr=curr.zero
                else:           # this means num bit is one. so we will add node one in the tree.
                    if not curr.one:
                        curr.one=TreeNode()
                    curr=curr.one
            curr.value=num      # after creating tree of num save the value of num at leaf level.
                                # we will use this in next process.
        
        # now tree is complete. 
        # Note that, all the leaf nodes are at the same level - always 32.
        
        # now we will traverse down the tree for each num in nums and find possible maximum value for each num.
        # travel tree to find max
        
        ans=0
        for num in nums:
            curr=root
            for i in bit:
                temp=1<<i
                check=temp&num  # this is check bit. if this is 0 means num bit is 0. 
                                # so xor value to be maximum we need bit to be 1.
                if check is 0:  # so if it's 0 we will go to node one.
                    if curr.one:    # if node one exists in tree go for it.
                        curr=curr.one
                    else:           # if not exists then only option left is to go for node zero.
                        curr=curr.zero
                else:           # if check bit is 1.if this is 1 means num bit is 1.
                                # so xor value to be maximum we need bit to be 0.
                    if curr.zero:  # so if node zero is exists in tree we go for it.
                        curr=curr.zero
                    else:       # if not then only option left is to go for node one.
                        curr=curr.one
            # now we have traverse the tree and we have number that can give us maximum value possible for num.
            ans=max(ans,num^curr.value) # update ans to maximum value possible.
        return ans
189965