Skip to content

112-path-sum#16

Open
05ryt31 wants to merge 1 commit intomainfrom
feat/112
Open

112-path-sum#16
05ryt31 wants to merge 1 commit intomainfrom
feat/112

Conversation

@05ryt31
Copy link
Copy Markdown
Owner

@05ryt31 05ryt31 commented Jan 12, 2026

問題リンク

https://leetcode.com/problems/path-sum/description/

問題文の概要

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.

A leaf is a node with no children.

次に解く予定の問題

https://leetcode.com/problems/binary-tree-level-order-traversal/description/

Comment on lines +27 to +28
checkPathSum(node.left, current_sum)
checkPathSum(node.right, current_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.

こうすると途中で hasTarget が見つかったとしても、計算が打ち切られずに続きますね。

Comment on lines +17 to +18
return (self.hasPathSum(root.left, targetSum - root.val) or
self.hasPathSum(root.right, targetSum - root.val))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらの or は False が見つかると計算を打ち切りますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

False が見つかると

True が見つかると、ですかね

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

おっとそうです。


def checkPathSum(node: Optional[TreeNode], current_sum: int):
nonlocal hasTarget
if node is None or hasTarget:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これらは (node が null) と (ターゲットを発見済み) という別々の条件なので、私だったら2つ別のif文にしますが、ひとまとまりにしてしまう方も多くいらっしゃるとは思います。

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.

確かに分けた方がよりそれぞれの目的が明確になりますね


self.checkPathSum(root, current_sum)を呼ぶタイミングがどこが適切かがわからない。今の場合だとまだ定義されていないから呼べないよと怒られる。

hasTarget = (current_sum == turgetSum)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

hasTarget = hasTarget or (current_sum == targetSum)
がやりたかった事ですかね?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

記号だけでやると|=ですね。


leafで判定するとより良い¥

合計を渡す代わりに残りのtargetを減らすやり方の方が賢かった
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これは、引き継ぐ情報として合計か、合計の補数のようなものを選ぶか、というような考え方に抽象化して考えられます。
引き継ぐ情報の量を減らすためによくある考え方と思います。これの自然な発想法としては、Treeの方を小さくして次に渡すので、合計の方も小さくして次に渡す、といった考え方もあります。

@@ -0,0 +1,31 @@
# step1を修正したVersion
# Time Complexity: O(n)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この表記を見てしばらく、最悪は容易だが平均がこうなるのを示すのは少し難しいかも?などと色々考えていたのですが、実は木の形を固定した場合にTrue/Falseや見つかる位置などと計算量がほぼ無関係なので、どんな場合を考えてもO(n)ですね。(感想です)


def checkPathSum(self, node: Optional[TreeNode], current_sum: int):
# leafまで辿り着いた時がnode is None、この時のcurrent_sumがtargetSumと一致しているかをチェック
if node is None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

early return してネストを下げたほうが読みやすいかなと思いました。

if node is None:
    hasTarget = (current_sum == targetSum)
    return

current_sum += int(node.val)
self.checkPathSum(node.left, current_sum)
self.checkPathSum(node.right, current_sum)

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.

6 participants