Skip to content

Create 101. Symmetric Tree.md#18

Open
Kitaken0107 wants to merge 2 commits intomainfrom
Kitaken0107-patch-20
Open

Create 101. Symmetric Tree.md#18
Kitaken0107 wants to merge 2 commits intomainfrom
Kitaken0107-patch-20

Conversation

@Kitaken0107
Copy link
Owner

# self.right = right
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
def check_mirror_image(root1,root2):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

, のあとに 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 あたりがよいと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

・ほかの問題でもそうでしたが、,で詰めないのは意識します
・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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not root1 and root2 or root1 and not root2: としたいところなのですが、逆に分かりずらいように思います。
if (not root1 and root2) or (root1 and not root2): あたりが良いかもしれません。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XOR にしたいならば、
(not root1) != (not root2)
あるいは、両方 None の場合がもうないので、
not root1 or not root1
でいいのではないでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。
not root1 or not roo2がこの場合だと一番直感的に感じました。

return True
if root1 == None and root2 != None or root1 != None and root2 == None:
return False
if:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid syntax だと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらと一つ下でいただいたコメントなので、
本来は以下で記載していたはずなのですが、間違いて載せてしまっていたようでした。
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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val が等しいかを調べるコードが抜けているように思います。

return False
if root1.val != root2.val:
return False
else:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私は、この else 消して、インデント下げます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants