Skip to content

112 path sum - #25

Open
MA-yo-TA wants to merge 3 commits into
mainfrom
112-Path-Sum
Open

112 path sum#25
MA-yo-TA wants to merge 3 commits into
mainfrom
112-Path-Sum

Conversation

@MA-yo-TA

Copy link
Copy Markdown
Owner

Comment thread 112-Path-Sum/note.md
Comment on lines +16 to +20
- https://github.com/naoto-iwase/leetcode/pull/29#discussion_r2455081026
- 「もし葉でtargetSumになるような経路を返却するとしたらどうでしょうか?設問としてはTrue/Falseなのですが、単純にTrue/Falseを得るよりはpathを実際に知るほうが意味があるのかなと思ったので...自分が面接だったら質問しそうです。」
- 葉から根へ向かうのは親を辿っていけば一本道でたどり着くので、探索しながら各ノードの親を覚えておく `node_to_parent: dict[TreeNode, TreeNode` 辞書を作って、葉から順番に辿っていけば良さそう。
- 全部のパスを返す必要があるなら、見つかった時点での return をやめて全ノードを探索すれば良い。
- パスを一つだけ見つければ良いという条件で再帰で書くなら↓のようになるだろうか。空リストより None が良いかもしれないのと、パスを探すならパスの存在自体が T/F の代わりになるので bool を返す必要はなさそう。全部のパスを返す場合は、返すのがパスのリスト = ノードのリストのリストになってだいぶ煩雑な気がする。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この議論はいいですね.勉強になりました.

Comment thread 112-Path-Sum/note.md
Comment on lines +64 to +66
return self.hasPathSum(root.left, children_target_sum) or self.hasPathSum(
root.right, children_target_sum
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

pythonに詳しくないのですが,こういう改行のほうが嬉しいかなと思いました.
pythonだと意味が変わっちゃうかもしれないのですが.

Suggested change
return self.hasPathSum(root.left, children_target_sum) or self.hasPathSum(
root.right, children_target_sum
)
return self.hasPathSum(root.left, children_target_sum) or
self.hasPathSum(root.right, children_target_sum)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。私も同じようなことは思ったのですが、ruff という標準的なフォーマッター(兼リンター)の一つをデフォルト設定で使ってこのようになるので今回はこれを採用しました。

見た目でいうと以下のようなものがよさそうでしょうか。(ただしこれも ruff でフォーマットすると元のコードに戻されます)

  return (
      self.hasPathSum(root.left, children_target_sum)
      or self.hasPathSum(root.right, children_target_sum)
  )

細かいところを補足しますと、

という感じのようです。

Comment thread 112-Path-Sum/step3.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

再帰とiterative, 両方書いていてよいなと思いました.

全体的に読みやすかったです

Comment thread 112-Path-Sum/note.md
他の方のコードでよく見かける `forntiers` という変数名を使ってみた。

再帰でも書ける(step1-2.py)。一本道だと再帰呼び出しの回数がノード数(最大 5000)になるので、注意が必要(Python の再帰呼び出し回数の上限はデフォルトだと 1000)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

余裕があれば,実際にかかる時間やメモリを見積もってみてもいいかもしれません

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。余裕のある時は見積もるようにします

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.

2 participants