Skip to content

Symmetric Tree#51

Merged
rihib merged 1 commit intomainfrom
symmetric_tree
Feb 24, 2025
Merged

Symmetric Tree#51
rihib merged 1 commit intomainfrom
symmetric_tree

Conversation

@rihib
Copy link
Owner

@rihib rihib commented Dec 18, 2024

Symmetric Treeを解きました。レビューをお願いします。

問題:https://leetcode.com/problems/symmetric-tree/
言語:Go

Kitaken0107/GrindEasy#18

Comment on lines +19 to +20
queue.PushBack(root.Left)
queue.PushBack(root.Right)
Copy link

Choose a reason for hiding this comment

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

私は、この2つずつ入っている、という方法はあまり好みません。
本当に2つずつ入っているのか、コード全体を見ないと分かりません。
コンパイル時の型チェックが効きません。

少々面倒でも struct 作ったほうがいいんじゃないでしょうか。

Choose a reason for hiding this comment

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

同意見です。型レベルでペアであることが保証されているとバグが起きにくそうです。

*/

/*
初見で解くことができなかったので、他の解法を見て解いた。再帰の練習が足りてなさそう。
Copy link

Choose a reason for hiding this comment

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

再帰は仕事の分担でよく説明していて一定の成果が出ているように見えていますが、混乱している気持ちはあんまり分かっていません。なんか余計なことを考えているようには見えます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

再帰自体は混乱せずに理解はできていてコードは読めばすぐに理解できたのですが、この問題の解法(どのようにしてsymmetricかを判断するか)自体が思いつかなかった感じです

Comment on lines +21 to +24
if root == nil {
return true
}
return isMirror(root.Left, root.Right)

Choose a reason for hiding this comment

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

個人的にも nil のときには true が自然だと思います。その場合、以下のようにすると最初の分岐が不要になりそうです。

Suggested change
if root == nil {
return true
}
return isMirror(root.Left, root.Right)
return isMirror(root, root)

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 left == nil || right == nil {
return false
}
return left.Val == right.Val && isMirror(left.Left, right.Right) && isMirror(left.Right, right.Left)

Choose a reason for hiding this comment

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

少し1行が長くなっていて、かつ「現在着目しているノードの話」と「子ノードの話」が含まれた条件になっているので、個人的には分割したほうがスッキリする気がします。

Suggested change
return left.Val == right.Val && isMirror(left.Left, right.Right) && isMirror(left.Right, right.Left)
if left.Val != right.Val {
return false
}
return isMirror(left.Left, right.Right) && isMirror(left.Right, right.Left)

Comment on lines +19 to +20
queue.PushBack(root.Left)
queue.PushBack(root.Right)

Choose a reason for hiding this comment

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

同意見です。型レベルでペアであることが保証されているとバグが起きにくそうです。

@rihib rihib merged commit 19bd868 into main Feb 24, 2025
@rihib rihib deleted the symmetric_tree branch February 24, 2025 07:26
rihib added a commit that referenced this pull request Mar 31, 2025
rihib added a commit that referenced this pull request Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants