0

I was doing a Leetcode question and I'm new to Python 3 and I found myself seriously confused about this after watching a Youtube video and Googling.

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        if not p and not q:
            return True
        if not p or not q or p.val != q.val:
            return False
        
        return(self.isSameTree(p.left, q.left) &
        self.isSameTree(p.right, q.right))
       

What does the Optional[TreeNode] mean in the isSameTree function? At first I thought it was an optional array of TreeNode objects but this is totally not right. The way I wrote my code I treated variables p and q as if they were root nodes to my tree but how do I know they're root nodes?

And why does Leetcode represent the input the question as an array if it really gives you a TreeNode (root?) object? See this image if you don't understand.

Edit: Making this edit to let you all know this isn't a duplicate question, it's slightly different than what has been suggested in the comments because it pertains to an object of TreeNode and is related to inputs of trees in Leetcode so it may be beneficial to posterity.

Esau
  • 41
  • 1
  • 6
  • 3
    `Optional[X]` just means the argument is of type `X` or `None` -- see the [Python typing docs](https://docs.python.org/3/library/typing.html#typing.Optional) –  May 29 '22 at 22:02
  • @mkrieger1 No this is a slightly different question but thank you for the suggestion – Esau May 29 '22 at 22:09
  • 2
    Also you shouldn't have "subquestions". You should have *one* question per post. – mkrieger1 May 29 '22 at 22:16
  • sorry saying sub-question isn't accurate, this question pertains to a Leetcode problem and could help posterity. And I still don't understand why the input is the way it is in the problem. Please keep this question up – Esau May 29 '22 at 22:18
  • Could you at least link to the problem, so we can see it ourselves? It's hard to make out in your screenshot. – CrazyChucky May 29 '22 at 22:20
  • Sure, I'll add it now – Esau May 29 '22 at 22:20
  • Still, it's unclear why "p: Optional[TreeNode] means that p is either a TreeNode or None" does not answer your question. It's irrelevant what TreeNode is to understand what Optional[] means. – mkrieger1 May 29 '22 at 22:22
  • I know understand that but I don't understand why the input is put as an array in the example but behaves like a root node? This is related to my question. – Esau May 29 '22 at 22:24
  • 1
    But that is a *different* question. Then you should make two separate posts. – mkrieger1 May 29 '22 at 22:24
  • If your question is "*Why* do they specify the parameters as `Optional[TreeNode]`", that's a different question... but there's only so authoritatively it could be answered without reading the mind of whoever wrote the question and decided to do it that way. – CrazyChucky May 29 '22 at 22:25
  • 1
    "At first I thought it was an optional array of TreeNode objects but this is totally not right." It is an optional TreeNode. Python doesn't have a concept like `void` in C, so if something is "optional" then we use the actual existing object `None` to represent the case where a "real" value isn't provided. The `[]` are there so that the base `Optional` knows what kind of thing is optional. – Karl Knechtel May 29 '22 at 22:25
  • 3
    The question says _given the root nodes_. The example q/p is just given to represent the values in the tree and the result, since giving the actual tree as code wouldn't be very concise. It's just an example of what a "tree" could be, not necessarily the actual input to your method (it's also giving `true` back). – MatsLindh May 29 '22 at 22:26
  • "I don't understand why the input is put as an array in the example but behaves like a root node?" I don't know what you mean. What "input" are you talking about? If you mean the fact that `[]` symbols are used in the `Optional[TreeNode]` syntax, then it has **nothing to do with** "arrays" (we call them lists in Python). – Karl Knechtel May 29 '22 at 22:26
  • @MatsLindh Thank you this answers my question. It does say it clearly there I didn't catch that because I was too busy at looking at the example input which made me think that it would be an array. – Esau May 29 '22 at 22:27
  • 1
    " The way I wrote my code I treated variables p and q as if they were root nodes to my tree but how do I know they're root nodes?" This question does not make sense because *"root" nodes are not a special or different kind of node*. *Every* node "is a root" of the subtree below it. Even the leaves. The code compares whether the nodes starting at q and p, and descending from there, are the same. It *does not matter* whether there are nodes "above" q and/or p. – Karl Knechtel May 29 '22 at 22:27
  • KarlKnechtel, the "input" I was referring to is the input from the example from the leetcode problem sorry if that is not clear. @MatsLindh Got what I was saying and answered my question but thank you – Esau May 29 '22 at 22:30

0 Answers0