Why the expected output of [1, 2] is [1, 2] instead of [1]?


  • 1
    C

    This is my python code

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param {TreeNode} root
        # @return {integer[]}
        def __init__(self):
            self.res = []
    
        def rightSideView(self, root):
            if not root:
                return self.res
            self.res.append(root.val)
            def trav(node):
                if node:
                    self.res.append(node.val)
                    trav(node.left)
                    trav(node.right)
            trav(root.right)
            return self.res
    

    The result
    Input: [1,2]
    Output: [1]
    Expected: [1,2]

    What is wrong?


  • 0
    L

    I have the same issue; if I test my code for the tree
    1 (root)

    2 (right child)
    I get the answer [ 1, 2]; but for some reason I can't pass the grader at around 6x test with the Input [1,2].Same thing as ChesterL, I got the Output [1], Expected [1,2]. My implementation is in C++


  • 0
    L

    For example:
    Given the following binary tree,

    1 <---
    /
    2 3 <---
    \
    5 4 <---

    You should return [1, 3, 4].

    Actually, maybe the expected answer should be [1, 3, 4, 5], i.e., all right nodes in the tree? Just a clarification would be nice.


  • 0
    C

    so the test case is not correct?


  • 0
    L

    Chester,

    I believe the test cases are correct. My solution (very similar to yours) does not pass the next test case (70), either. Since there are a lot of people who passed the grader, I believe it is just the way that we both understand what we suppose to do is incorrect. Apparently the description is misleading. I will check how other people implemented it and I will adjust my implementation. Best.


  • 0
    C

    I got it. we are seeing the tree from the right side, so because there is only one node on level 2, which is 2, we can see it anyway, the answer to [1, 2] is [1, 2]


Log in to reply
 

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