Wrong answer in the only testcase

Tree tree = new Tree();

You are doing this only once in the beginning. So, all the nodes of all the test cases are getting added to the same tree. A new tree has to be created for every test case.