Conversation
| # self.right = right | ||
| class Solution: | ||
| def isSymmetric(self, root: Optional[TreeNode]) -> bool: | ||
| def check_mirror_image(root1,root2): |
There was a problem hiding this comment.
, のあとに 1 つスペースを空けましょう。
https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements
https://google.github.io/styleguide/pyguide.html#s3.6-whitespace
mirror image で鏡像を表そうとしているのだと思うのですが、直感的ではないように思います。
個人的には is_symmetric() としたいですが、エントリーポイントの関数名と被るので微妙にも感じます。 check_symmetric() あたりでいかがでしょうか?
引数の変数名に root という単語を使っていますが、辿っていくうちに root ではなくなるため、違和感があります。 node1 と node2、もしくは left と right あたりがよいと思います。
There was a problem hiding this comment.
・ほかの問題でもそうでしたが、,で詰めないのは意識します
・check_symmetric()、直感的でわかりやすいです。ありがとうございます。
・root1,2あまり深く考えず使っていました。たしかにおっしゃる通りです。
ほかのlinkedlistの問題で、headを進み続けたときの違和感と同じですね。
・node1.2、left・right良いですね。承知しました。
| class Solution: | ||
| def isSymmetric(self, root: Optional[TreeNode]) -> bool: | ||
| def check_mirror_image(root1,root2): | ||
| if root1 == None and root2 == None: |
There was a problem hiding this comment.
if not root1 and not root2: のほうがシンプルに感じます。
| def check_mirror_image(root1,root2): | ||
| if root1 == None and root2 == None: | ||
| return True | ||
| if root1 == None and root2 != None or root1 != None and root2 == None: |
There was a problem hiding this comment.
if not root1 and root2 or root1 and not root2: としたいところなのですが、逆に分かりずらいように思います。
if (not root1 and root2) or (root1 and not root2): あたりが良いかもしれません。
There was a problem hiding this comment.
XOR にしたいならば、
(not root1) != (not root2)
あるいは、両方 None の場合がもうないので、
not root1 or not root1
でいいのではないでしょうか。
There was a problem hiding this comment.
ありがとうございます。
not root1 or not roo2がこの場合だと一番直感的に感じました。
101. Symmetric Tree.md
Outdated
| return True | ||
| if root1 == None and root2 != None or root1 != None and root2 == None: | ||
| return False | ||
| if: |
There was a problem hiding this comment.
こちらと一つ下でいただいたコメントなので、
本来は以下で記載していたはずなのですが、間違いて載せてしまっていたようでした。
if root1.val != root2.val:
return False
else:
return check_mirror_image(root1.left,root2.right) and check_mirror_image(root1.right,root2.left)
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: |
| return False | ||
| if root1.val != root2.val: | ||
| return False | ||
| else: |
https://leetcode.com/problems/symmetric-tree/