public class Solution {
public int distributeCandies(int[] candies) {
return Math.min(candies.length / 2, ((int) Arrays.stream(candies).distinct().count()));
}
}

Wow really impressed by your merkle tree solution. Genius!

For this problem though, collision resistant hash functions like sha256 are not necessary, from performance perspective. You can use some computationally cheaper hash functions. With an addition of O(|T|) checking every time hash values match, correctness is also made sure.

public int distributeCandies(int[] candies) {
java.util.BitSet bs = new java.util.BitSet(200001);
int max = candies.length >> 1;
int cnt = 0;
for (int i : candies) {
i += 100000;
if (bs.get(i)) {
cnt++;
}
bs.set(i);
}
return Math.max(cnt, max);
}

Can anyone tell me the time Complexity of this Code ?
I am thinking it as O(n2) because both the functions are traversing the tree.
And also how to compute time complexity of this kind of recursive functions ?

@compton_scatter
Dude I got a question, I remember that different trees may have the same preorder traversal string, does that works for traversal strings that records the "null" node? because if so there maybe faulty in your algorithm.