backtobackswe / backtobackswe-feedback

1 stars 0 forks source link

Test If A Binary Tree Is Symmetric #2

Closed dariuscox closed 3 years ago

dariuscox commented 3 years ago

Back To Back SWE



Create a bug report to help us improve our content -



Your email registered on BackToBackSWE :

dariuscox (github)



Category of the bug :



Description of the bug :

Invalid solution accepted for this problem. Should maybe add another testcase to fail this solution.



Code you used for Submit/Run operation :

class Solution:
    def isSymmetric(self, root):
        '''
        :type root: TreeNode
        :rtype: bool
        '''
        if not root:
            return True

        traversal = self.inorder_traversal(root)
        if len(traversal) % 2 == 0:
            return False
        i = 0
        j = len(traversal)-1

        while i<=j:
            if traversal[i] != traversal[j]:
                return False
            i += 1
            j -= 1
        return True

    def inorder_traversal(self, node):
        traversal = []

        if node:
            traversal = self.inorder_traversal(node.left)
            traversal.append(node.val)
            traversal = traversal + self.inorder_traversal(node.right)
       # else:
       #     traversal.append('n')
        return traversal



Language used for code :

Python



Expected behavior :

This solution with the commented out else statement should fail this problem in the case of a tree where the child nodes are the same but in incorrect positions. For example:

         4
       /   \
      2     2
     /     /
    1    1

Unless I misunderstood the question, the solution here (which was accepted) would say this is symmetric when it is not. Because I only check if the values are the same left and right but not if the positioning is symmetric. Uncommenting the else statement would catch this edge case.



Screenshots :



Additional context :



shubhankar-99 commented 3 years ago

Thank You for your feedback. The output to your test case should be false which is also the output of your code. So it is getting accepted. You can also check your code for custom test cases using "Single Testcase" feature on our website