one test case is failing for the code. Could you help me understand where is the issue in code. It got accepted on Leetcode.
Could you check FormTree function. I believe there could be an error.
one test case is failing for the code. Could you help me understand where is the issue in code. It got accepted on Leetcode.
Could you check FormTree function. I believe there could be an error.
Okay I will check for error. Is it passing all testcases on leetcode for the same question?
Also can you post the leetcode question link?
yes it is passing. I mentioned in the doubt. Please find the leetcode link for the question.
It is showing this error for the test case
Exception in thread “main” java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
at Main.formTree(Main.java:97)
at Main.main(Main.java:26)
This could be because of a compiler issue used by coding blocks where it first checks the semantics and code later. To workaround your way use this
BinaryTreeNode peek = new BinaryTreeNode(0);
if(!stack.isEmpty()){
peek = stack.peek();
}
This will create a node with o as data as peek and obviously it will have left and right child as null. So the whole process will be done. So this will give wrong answer.
I don’t think we should create a node when stack is empty. Although, I don’t see any case when stack would be empty. Can you give me the test case which is causing this error. May be there is some issue with the test case.
23 11 3
1 2 4 8 -1 -1 9 -1 -1 5 10 -1 -1 11 -1 -1 3 6 -1 -1 7 -1 -1
It’s working fine on this test case. Please check again.
Give me the test case where it is returning wrong answer. I don’t see any run error for this test case.
All other test cases apart from this one are passing. This one was not passing on my side. If it is now then it must have been some problem on the backend. Code is correct
I am not sure about it is passing or not. one test case is giving me wrong answer. But I am not getting any run error as u mentioned.
The function that I have written to construct tree from preorder may have some issue when a node only has one child.
Can you share the code just to construct a tree from preorder given??
You need at least two orders(pre, post, in) to be able to construct a tree. You cannot construct from a single traversal approach
But a tree needs to be created in order to find the node first marked.
As per the question , when -1 was encountered I assumed it as null and created a tree in preorder fashion. This way tree can be created. I did it by this way :
private static BinaryTreeNode createTree() {
int data = scanner.nextInt();
if(data == -1)
{
return null;
}
BinaryTreeNode root = new BinaryTreeNode(data);
root.left = createTree();
root.right = createTree();
return root;
}
It is working now for all test cases. It was too late to hear from you. Expect a more swift response in future. Thanks 
Yeah apologies for the delay, this doubt had suddenly vanished from my account, and I only received the credentials back yesterday for this doubt