31ms O(n) Java solution using Trie
We add the number into the trie and find the max possible XOR result at the same time.\nNode.set() method will set the new node in the trie if needed and return
We add the number into the trie and find the max possible XOR result at the same time.\nNode.set() method will set the new node in the trie if needed and return the new node.\nAfter setting the node, find the opposite bit in the trie to maximize the possible XOR result.\n\n```\npublic class Solution {\n    public class Node {\n        Node one;\n        Node zero;\n        Node() {\n            this.one = null;\n            this.zero = null;\n        }\n        Node set(int n) {\n            if (n == 0) {\n                if (this.one == null) this.one = new Node();\n                return this.one;\n            }\n            if (this.zero == null) this.zero = new Node();\n            return this.zero;\n        }\n    }\n    \n    public int findMaximumXOR(int[] nums) {\n        Node root = new Node();\n        int re = 0;\n        for (int num : nums) {\n            int digit = num;\n            int tmp = 0;\n            Node setNode = root;\n            Node findNode = root;\n            int pos = 30;\n            while (pos >= 0) {\n                digit = (num >> pos) & 1;\n                setNode = setNode.set(digit);\n                if (digit == 1) {\n                    if (findNode.one != null) {\n                        tmp = tmp | (1 << pos);\n                        findNode = findNode.one;\n                    } else {\n                        findNode = findNode.zero;\n                    }\n                } else {\n                    if (findNode.zero != null) {\n                        tmp = tmp | (1 << pos);\n                        findNode = findNode.zero;\n                    } else {\n                        findNode = findNode.one;\n                    }\n                }\n                pos--;\n            }\n            re = Math.max(re, tmp);\n        }\n        return re;\n    }\n}\n```
[]