-
Notifications
You must be signed in to change notification settings - Fork 0
112. Path Sum #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
112. Path Sum #27
Conversation
| if not root: | ||
| return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
root がないケースをコーナーケースとして取り出さなくても、ループの1回目の処理で L.141-142 で正しく判定できそうです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
脳内トレース漏れしてました。
| def is_leaf(node: Optional[TreeNode]) -> bool: | ||
| assert node | ||
| return not node.left and not node.right |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node が None であるケースは例外(Exception)ではないと思うので、シンプルに False を返すのが適切かなと思いました。引数の型は Optional[TreeNode] で None を許容しているにも関わらず、それを入れると例外が返ってくるというのはびっくりするかと。葉であるかの判定を関数化するなら以下のようなコードがよさそうです。
| def is_leaf(node: Optional[TreeNode]) -> bool: | |
| assert node | |
| return not node.left and not node.right | |
| def is_leaf(node: Optional[TreeNode]) -> bool: | |
| return node and not node.left and not node.right |
また上記を採用する場合、L.141-142 の判定も不要になりますね。(合わせて remain -= node.val でなく L.145 内で remain - node.val == 0 のように扱う必要もありますが)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご指摘ご尤もですね。
型への意識が弱いので、もう少し慎重にする必要がありそうです。
実装例もありがとうございます。
| if not root: | ||
| return False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここも不要ですかね
| if not node: | ||
| continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ただの選択肢の話ですが、node.left or right がNoneでないときだけstackに積むようにすればここ不要になりますね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
今回は突っ込む方式にしました。
| if is_leaf(node) and remain == 0: | ||
| return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if is_leaf(node):
return remain == 0とすると、nodeが葉の場合に無駄にleft, rightをスタックに入れずに済む気がしました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
ほんとですね。。こういうのってパッと見てすぐ気づくものなのでしょうか(どう意識したら良いのかと)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ん、ループでこれやるとまずくないですかね。
最初の葉が見つかった時に残数が0で無ければ探索打ち切っちゃいますよね?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あっ本当ですね...失礼しました 🙏
step4追加
| if not root.left and not root.right and root.val == targetSum: | ||
| return True | ||
|
|
||
| return self.hasPathSum(root.left, targetSum - root.val) or \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
binary operatorの前で改行を入れると良さそうです。
https://peps.python.org/pep-0008/#should-a-line-break-before-or-after-a-binary-operator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
またやってしまいました(2回目)
これは癖になっているので意識しないとです。ありがとうございました。
| # self.right = right | ||
| class Solution: | ||
| def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| def is_leaf(node: TreeNode) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かいですが、個人的にはclassのprivateメソッドとして定義したいです。汎用的なためhasPathSumが呼び出されるたびにis_leafの関数オブジェクトを作成する必要はないように感じました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、確かにそうですね。
こういうご指摘を頂けるのもこの勉強会ならではですね。
| if not node: | ||
| continue | ||
|
|
||
| remain -= node.val |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
細かいですが、remainって自動詞なのでremained,leftとかrestとかが適切なのかなと
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
軽く調べたつもりだったんですけど、remainsにしないといけないんですね。
しかも不可算名詞だから相応しくないですね。。
https://leetcode.com/problems/path-sum/description/