Skip to content

98. Validate Binary Search Tree#27

Open
tom4649 wants to merge 4 commits intomainfrom
98.Validate-Binary-Search-Tree
Open

98. Validate Binary Search Tree#27
tom4649 wants to merge 4 commits intomainfrom
98.Validate-Binary-Search-Tree

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 18, 2026


class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def isValidBST_with_range(node, lower_bound, upper_bound):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LeetCodeで与えられているものに引数を加えたバージョン、という意図かと予想しますが、変数名は基本的に snake_case にした方が良いと思います。

https://peps.python.org/pep-0008/#function-and-variable-names

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

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.

次回からはこのような場合でもsnake_caseにしようと思います。

def isValidBST_with_range(node, lower_bound, upper_bound):
if node is None:
return True
if node.val <= lower_bound or node.val >= upper_bound:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ただの感想ですが個人的には step 2 のように、 (nodeが範囲内) の否定、の方がコードと脳内での言語化の距離が近くてわかりやすいように思います。

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.

そうですね、自分もそう思ってsol2でロジックを変えました

frontiers.append((node.left, lower_bound, node.val))
if node.right is not None:
frontiers.append((node.right, node.val, upper_bound))
return 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.

個人的な好みですが、whilte や for 文などの後には一つ空行を入れるようにしています。たまに目が滑って、while 文中だと勘違いしてしまうので 😓

Copy link
Copy Markdown
Owner Author

@tom4649 tom4649 Mar 25, 2026

Choose a reason for hiding this comment

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

なるほど、勉強になります。他の方でもこのようにされている方は多そうですね。
https://peps.python.org/pep-0008/#blank-lines
にも関連しそうなことが書かれていますね。

return True

frontiers = [(root, -float("inf"), float("inf"))]
while frontiers:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

frontier を単数形にするか複数形にするかは議論があるようですね。私も過去指摘されたことですが、もしまだ目を通されていないなら、Discord内のレビューを見てみると良いかもしれません。

achotto/arai60#6 (comment)

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.

単数形の方が良さそうです。次回以降こちらを採用します。
合わせてこのfrontierという名前に慣れておこうと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この解法、[1,2] とかで落ちませんか?

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.

修正しました。修正前のコードではテストに通りませんでした(違うコードをpushしてしまったようです)。

Comment on lines +19 to +23
return isValidBST_with_range(
node.left, lower_bound, min(upper_bound, node.val)
) and isValidBST_with_range(
node.right, max(lower_bound, node.val), upper_bound
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

minmax は取るまでもなく結果が分かっていそうです。

Suggested change
return isValidBST_with_range(
node.left, lower_bound, min(upper_bound, node.val)
) and isValidBST_with_range(
node.right, max(lower_bound, node.val), upper_bound
)
return isValidBST_with_range(
node.left, lower_bound, node.val
) and isValidBST_with_range(
node.right, node.val, upper_bound
)

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.

上段でチェックしているので、その通りですね。修正しました。

frontiers = [(root, -float("inf"), float("inf"))]
while frontiers:
node, lower_bound, upper_bound = frontiers.pop()
if not lower_bound < node.val < upper_bound:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

lower_boundupper_bound はinclusiveなのかexclusiveなのか分かりにくいので、_exclusive と変数名に明示するか、must_be_less_than のような直接的な変数名にしたいです。

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.

must_be_less_than、良い名前だと思ったので採用します

def push_it_and_left_children(node):
if node is None:
return
while node.left is not 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.

末端を積みそこねてしまいそうです。

Suggested change
while node.left is not None:
while node is not None:

Comment on lines +30 to +31
if node.right is not None:
push_it_and_left_children(node.right)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

push_it_and_left_children 内でNoneチェックをしているので、ここでは不要ですね。

Suggested change
if node.right is not None:
push_it_and_left_children(node.right)
push_it_and_left_children(node.right)

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