My Code Shows wrong answer for the input 5 1 1 -1 -1 -1
The Correct answer is 0
My Code Output is 1
Someone Please Explain why this is not symmetric.
Isn,t the inorder traversal of the symmetric tree will be palidromic in nature?
During Inorder Traversal, when you encounter a NULL TreeNode, just push_back(-1) to the vector you are storing Inorder Traversal in. Then your code should work.
I have the same doubt. 5 1 1 -1 -1 -1 must be symmetric, My code fails this test case.
Include the depth of the node in your vector and check for the palindromic list. Then you will get correct answer.
5 1 1 -1 -1 -1 is not symmetric because root node is 1 and there is only 1 left child with value 1 and there is no right child so it’s not symmetric.
I used inorder traversal in python , I am getting time limited exception.
After computing inorder, just add a condition that if size of inorder is even then return false.
No, it is not symmetric. The first value(5) is not a part of the tree. It tells how many values to read in the input stream. The tree looks like this:
1
/ \
1 NULL
/ \
NULL NULL
there can be multiple binary tree for an inorder traversal.
eg-[1,3,2,-1,2,-1,3] inorder traversal is palindromic but not symmetric .
symmetric trees have palindromic inorder traversal but not vice versa.