# Why Input:{1,3} output can not be {1,3} ?

• My code is as below. I got error:
Input: {1,3}
Output: {1,3}
Expected: {3,1}

Why Output:{1,3} is wrong? Thanks.

``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
return nullptr;
}
while(s2->next != nullptr && s2->next->next != nullptr) {
s1=s1->next;
s2=s2->next->next;
prev = s1;
}
s2 = s1->next;
prev->next = nullptr;
//two nodes case: {1,3}. head should be nullptr, s2 is the second node
}
}
TreeNode *root = new TreeNode(val);
while(s2->next != nullptr && s2->next->next != nullptr) {
s1=s1->next;
s2=s2->next->next;
prev = s1;
}
s2 = s1->next;
prev->next = nullptr;
}
} else {
root->left =  nullptr;
}
while(s2->next != nullptr && s2->next->next != nullptr) {
s1=s1->next;
s2=s2->next->next;
prev = s1;
}
s2 = s1->next;
prev->next = nullptr;
}
} else {
root->left =  nullptr;
}
return root;
}

};``````

• (1, 3) is wrong because 3 is the left child of 1. This is apparently not a BST. You can pass with (1, #, 3) though.

• Thanks a lot for the help. I found the bug: in the last 3th line, it should be "root->right= helper(head2, s2, s1->val);" rather than "root->left = helper(head2, s2, s1->val);"
With this change, now I got error: Output Limit Exceeded.
What does this error mean? Thanks.

• For this problem, it means the output tree contains more nodes than it should have had. You may want to double check thw logic.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.